gpt4 book ai didi

android - 为 Nav Drawer 中的 Android 状态栏着色

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:55:20 24 4
gpt4 key购买 nike

在我正在构建的这个应用程序中,我在我的 Activity 中添加了一个 Navigation Drawer fragment 。我使用的是 5.0,所以我已经能够设置 primaryColor 和 primaryColorDark 以获得正确的颜色。我决定尝试将我的 Nav Drawer 设计为与 Google Now 5.0 中的抽屉非常相似,其中 Nav Drawer 有自己的状态栏背景。 (发不了图片,声望不够>.>)

我已经遵循了这个问题的建议 here ,这有很大帮助。当我拉出抽屉导航时,我已经在状态栏下绘制了抽屉导航。唯一的问题是,我不知道如何设置抽屉中显示的状态栏的颜色。目前,它只显示白色。

以下是我的样式和代码的相关部分:

Activity 布局:

<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>

fragment 布局:

<?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>

根据上面的链接,为 Activity 设置状态栏的颜色(这部分工作正常):

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));

// ....
}

最后,我为 Activity 关联的样式,以及我“尝试”分配给 fragment 的内容。

<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 的主屏幕如何具有透明状态栏有关,并且仅在 Nav Drawer 期间为其着色,但任何相反的消息都会很棒!

最佳答案

根据您的设置,Nav Drawer 只是一个在状态栏(颜色其中您已通过 StatusBarActivityTheme 设置为透明)。系统只看Activity主题设置状态栏颜色;将 android:statusBarColor 分配给 child 无效。

简单的解决方案是将 Nav Drawer fragment 布局的 android:background 更改为您想要的颜色。如果您希望图像下方的部分保持白色,一个选项是在您的抽屉布局中添加一个带有 android:background="@color/WindowBackground" 或其他颜色的空 View 。


如果您希望抽屉导航中的内容延伸到状态栏下方,则需要做更多的工作。它开始偏移的原因是因为您将 android:fitsSystemWindows 属性设置为 true,这又会调用 View 的默认值 fitSystemWindows()。 .正如文档所解释的那样,此方法采用插图(即在我们的例子中状态栏的高度)并将其应用为 View 的填充(在你的例子中,这成为你的 RelativeLayout 的顶部填充抽屉导航 fragment )。

绕过此填充的一种方法是覆盖 View 的 fitSystemWindows() 方法。我将您引导至 Google 的开源 IO Schedule 应用程序 - 具体来说,他们使用了 ScrimInsetsScrollView作为其 Nav Drawer 中的根元素.此修改后的 ScrollView 应用您选择颜色的稀松布(通过自定义 app:insetForeground 属性设置)并消耗插图,即不更多填充!

ScrimInsetsScrollView 可以用作模型来为任何 View 后代编写您自己的版本,真的 - 看到几乎相同的 ScrimInsetsFrameLayout .

关于android - 为 Nav Drawer 中的 Android 状态栏着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26568428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com