【发布时间】:2016-11-07 02:01:47
【问题描述】:
我有一个DrawerLayout 的活动。我们的客户要求我们根据从DrawerLayout 中选择的项目来动态更改此活动的操作栏颜色和相应的状态栏颜色。这很容易做到。但是,我的问题是当我动态更改状态栏颜色时,我无法保持状态栏透明。当我打开抽屉时,彩色状态栏覆盖了DrawerLayout 的顶部,如下所示:
但是,我希望我的 DrawerLayout 看起来像这样:
我可以通过以下行来做到这一点:
<item name="android:windowTranslucentStatus">true</item>
不过,我的问题不是我不能设置状态栏的透明度。我的问题是状态栏和操作栏颜色的动态变化不适用于windowTranslucentStatus。即使在我调用getWindow().setStatusBarColor() 之后,我的状态栏颜色仍然是colorPrimaryDark(上图中状态栏上可见的芥末黄色)。
现在,我关注了 this 教程和 this 和 this stackoverflow 问题以及许多其他问题,但无法解决问题。所有这些文章都说,一旦我将windowTranslucentStatus 设置为true,ActionBar 将移动到状态栏下方的顶部(以便状态栏与操作栏重叠)。之后,我应该能够为操作栏添加一些填充,并且简单地更改操作栏颜色也会导致相同颜色的更暗状态栏,因为状态栏实际上是半透明的并且与我的操作栏重叠。但是,由于某种原因,这在我的情况下不会发生。无论我将fitsSystemWindows 设置为true 还是false,或者完全删除该属性,操作栏都会保持原样。操作栏总是低于状态栏,当然,如果我设置透明度,它总是黄色的。
在以编程方式更改状态栏颜色时,我还尝试将 alpha 设置为状态栏颜色。这确实使状态栏有点透明,但它看起来很奇怪,因为它不再是真正的 dark 了。删除CoordinatorLayout 也无济于事。我花了几个小时试图解决这个问题,现在很沮丧。任何帮助是极大的赞赏。
我的activity_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<include layout="@layout/nav_header_main" />
<android.support.v7.widget.RecyclerView
android:id="@+id/nav_menu_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/nav_header_height"
android:clipToPadding="false"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/size_14dp"
app:layoutManager="LinearLayoutManager" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
这是我的app_bar_main 的 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<FrameLayout
android:id="@+id/toolbar_container"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<com.quintype.sakshipost.Widgets.CustomMaterialSearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
【问题讨论】:
标签: android xml android-coordinatorlayout drawerlayout android-statusbar