【发布时间】:2014-10-25 23:49:59
【问题描述】:
在我正在构建的这个应用程序中,我在我的活动中添加了一个导航抽屉片段。我使用的是 5.0,所以我已经能够设置 primaryColor 和 primaryColorDark 以获得正确的颜色。我决定尝试将我的导航抽屉设置为与 5.0 中的 Google Now 抽屉非常相似,其中导航抽屉有自己的状态栏背景。 (不能发图,声望不够>.>)
我遵循了这个问题的建议here,这对我有很大帮助。当我拉出导航抽屉时,我已经在状态栏下绘制了它。唯一的麻烦是,我不知道如何设置抽屉中显示的状态栏的颜色。目前,它只显示白色。
以下是我的样式和代码的相关部分:
活动布局:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main"
android:fitsSystemWindows="true">
<!-- main content -->
...
<!-- Drawer fragment -->
<fragment
android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:theme="@style/DrawerTheme"
android:fitsSystemWindows="true"
android:choiceMode="singleChoice"
android:name="com.myname.appname.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
片段布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/navDrawerFragment"
android:theme="@style/DrawerTheme"
android:background="@color/WindowBackground"
tools:context=".NavigationDrawerFragment"
android:fitsSystemWindows="true">
<!-- content here -->
</RelativeLayout>
根据上面的链接,为我的活动设置状态栏的颜色(这部分工作正常):
public void onCreate(Bundled savedInstanceState) {
super.onCreate(savedInstanceState);
// ....
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, Gravity.START);
drawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
// ....
}
最后,我与活动相关联的样式,以及我“尝试”分配给片段的内容。
<style name="StatusBarActivityTheme" parent="MyAppTheme">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
<style name="DrawerTheme" parent="MyAppTheme">
<item name="android:statusBarColor">#000000</item>
<item name="android:colorPrimaryDark">#000000</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>
提前感谢您发送给我的任何帮助!我怀疑我的问题与 Google Now 的主屏幕如何具有透明状态栏有关,并且仅在导航抽屉期间对其进行着色,但任何相反的消息都会很棒!
【问题讨论】:
-
R.color.colorPrimaryDark 从您的应用 XML 引用颜色值,而不是 android:colorPrimaryDark 主题属性。你在 res 文件中如何定义 @color/colorPrimaryDark?
-
我已经将@color/colorPrimaryDark 定义为十六进制颜色,我想不出来。该部分正在工作 - 它正确地为活动(抽屉布局)状态栏着色。我更关心片段抽屉。 (我真的希望我可以发布一些屏幕截图,但这是我的第一篇文章......)
-
DrawerLayout.setStatusBarBackgroundColor() 是设置抽屉状态栏颜色的正确方法调用。您无需为其设置特殊主题。 getResources().getColor(R.color.colorPrimaryDark) 是否解析为白色?
-
R.color.colorPrimaryDark 在 colors.xml 中设置,声明如下:
#303f9f 。它为 Activity 的布局 (activity_main.xml) 中的顶部容器设置状态栏颜色。没有着色的是片段的状态栏部分。我意识到我可以使用 imgur,所以它看起来像这样:imgur.com/5rbKnZb。右边是主要内容,左边是片段导航抽屉。 -
哦,还有,这是我正在尝试实现的 Google Now 的效果。同样,左边是导航抽屉,右边是内容。 imgur.com/gC4s0YB
标签: android navigation-drawer android-theme android-5.0-lollipop android-appcompat