android - 如何使状态栏和工具栏与上面的抽屉颜色相同

标签 android navigation-drawer android-appcompat

这让我发疯。 我想要一个抽屉导航,它被透明的状态栏和与工具栏颜色相同的状态栏覆盖。就像这两张图片: Same colors Drawer

所以我将工具栏和状态栏颜色设置为相同的值。没关系,看起来符合预期。除了抽屉呈现在状态栏下方这一事实。 OK,大家都说:加半透明属性。所以我添加了 <item name="android:windowTranslucentStatus">true</item>到我的主题,但出现了一个奇怪的副作用 - 状态栏颜色变成了一些非常暗的颜色。

enter image description here

有什么想法吗?

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/

相关文章:

java - 在Android中使用Fragment时出现ClassCastException

php - Codeigniter user_agent 不工作

react-native - 结合 TabNavigator 和 Drawernavigator

android - Android Studio 1.4 中的抽屉导航模板

android-appcompat - 来自 support7Demos 的相同代码在 AppCompat DayNight Mode 23.2.0 的不同设备上表现不同

android - android.support.v7.preference.Preference 找不到 onCreateView() 方法

java - 从另一个应用程序启动应用程序的最佳方式是什么?

android - 如何处理长按 ListView 项目?

ios - Swift 3 和带抽屉导航的谷歌地图

android - 链接引用失败 - 在 appcompat 中找不到样式属性 '@android:attr/textColor'