我有一个活动Navigation Drawer
并使用ScrimInsetsFrameLayout
我能够将布局放在StatusBar
一切都很顺利。
然后我决定更换颜色Toolbar
and StatusBar
with a png
所有活动布局的背景。
我在模拟器(Nexus 5 with android 6.0)上运行该应用程序,结果正是我想要的,就像你可以在Image #1下面,但是当我在我的设备(带有 android 5.0 的 Galaxy Note 3)上尝试时,ScrimInsetsFrameLayout 内的布局超出了StatusBar
Image #2。
我不明白出了什么问题,你能帮助我吗?
这是我的values-v21
and my activity.xml
<style parent="Theme.AppCompat.Light.NoActionBar" name="AppTheme_Activities">
<item name="android:colorPrimary">@android:color/transparent</item>
<item name="android:colorPrimaryDark">@color/insetF</item>
<item name="android:navigationBarColor">@color/insetF</item>
<item name="android:colorAccent">@color/primary</item>
<item name="android:colorEdgeEffect">@color/primary</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:statusBarColor">@color/insetF</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>
<?xml version="1.0"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/drawer"
android:fitsSystemWindows="true"
android:background="@drawable/background"> <!--png image-->
<FrameLayout
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<include layout="@layout/toolbar_activities" android:id="@+id/toolbar_layout"/>
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/content_frame">
</FrameLayout>
</FrameLayout>
<com.example.myapplication.ScrimInsetsFrameLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearLayout"
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:insetForeground="#4000"
android:clickable="true"
android:background="#ffffff"> .....
</com.example.myapplication.ScrimInsetsFrameLayout>
</android.support.v4.widget.DrawerLayout>
Image #1
Image #2
-
添加到您的onCreate()
of Activity
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
Change ScrimInsetsFrameLayout
's android:fitsSystemWindows
财产给false
-
Remove android:fitsSystemWindows="true"
from DrawerLayout
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/drawer"
android:background="@drawable/background"> <!--png image-->
<FrameLayout
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<include layout="@layout/toolbar_activities" android:id="@+id/toolbar_layout"/>
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/content_frame">
</FrameLayout>
</FrameLayout>
<com.example.myapplication.ScrimInsetsFrameLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearLayout"
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="false"
app:insetForeground="#4000"
android:clickable="true"
android:background="#ffffff"> .....
</com.example.myapplication.ScrimInsetsFrameLayout>
</android.support.v4.widget.DrawerLayout>
-
将这些样式添加到您的AppTheme_Activities
主题(保持您的状态所需的颜色):
<item name="windowActionBarOverlay">false</item>
<item name="android:windowActionBarOverlay">false</item>
<item name="android:fitsSystemWindows">false</item>
<item name="android:statusBarColor">#4000</item>
-
这将导致工具栏也位于状态栏下方,因此您需要执行此操作:
-
Make Toolbar
的高度 ="wrap_content"
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
-
Set Padding
为了Toolbar
:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
......
......
public int getStatusBarHeight() {
int result = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
}
return result;
}
应该就是这样了!
我已将状态栏下方带有导航抽屉的测试应用程序的源代码上传到我的保管箱 -请随意查看。 https://www.dropbox.com/s/elnsloyozyp43u3/TranslucentStatusBarUpdated.zip?dl=0
我不久前回答过非常类似的问题 - 也许你也会发现它很有用:Android 中具有动态 ActionBar 颜色的半透明 StatusBar https://stackoverflow.com/a/34384963/1658267
我希望,它有帮助
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)