这让我发疯。 我想要一个抽屉导航,它被透明的状态栏和与工具栏颜色相同的状态栏覆盖。就像这两张图片:
所以我将工具栏和状态栏颜色设置为相同的值。没关系,看起来符合预期。除了抽屉呈现在状态栏下方这一事实。 OK,大家都说:加半透明属性。所以我添加了 <item name="android:windowTranslucentStatus">true</item>
到我的主题,但出现了一个奇怪的副作用 - 状态栏颜色变成了一些非常暗的颜色。
有什么想法吗?
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = requireView(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = requireView(R.id.drawer);
drawerLayout.setStatusBarBackgroundColor(R.color.primary);
}
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>
style.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:fitsSystemWindows">true</item>
</style>
</resources>
最佳答案
似乎 DrawerLayout
已经更新,那些说添加 android:windowTranslucentStatus
的教程已经过时了。
要使其在最新的支持库(现在是 23.1.0)中工作,请在 values-v21
中添加以下属性(如 CheeseSquare 中所示)。
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
您总能在the CheeseSquare sample 中找到支持库的使用模式,尽管我认为此类说明应该正式记录,而不是存在于博客文章和示例中。
关于android - 如何使状态栏和工具栏与上面的抽屉颜色相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32512292/