Android:具有动态actionBar颜色和DrawerLayout的透明状态栏

7 xml android drawerlayout android-statusbar android-coordinatorlayout

我有一个活动DrawerLayout.我们的客户要求我们根据从中选择的项目动态更改操作栏颜色和此活动的相应状态栏颜色DrawerLayout.这很容易做到.但是,我的问题是,当我动态更改状态栏颜色时,我无法保持状态栏透明.当我打开抽屉时,彩色状态栏覆盖了这样的顶部DrawerLayout:

错误的布局

但是,我希望我DrawerLayout看起来像这样:

正确的布局

我可以使用以下行:

<item name="android:windowTranslucentStatus">true</item>
Run Code Online (Sandbox Code Playgroud)

但是,我的问题不在于我无法设置状态栏的透明度.我的问题是动态更改状态栏和操作栏颜色不起作用windowTranslucentStatus.colorPrimaryDark即使我打电话,我的状态栏颜色仍然是(上图中状态栏上可见的芥末黄色)getWindow().setStatusBarColor().

现在,我跟着这个教程和这个这个在许多其他计算器的问题,但未能解决该问题.所有这些文章都说,ActionBar一旦我设置为windowTranslucentStatus,将移动到状态栏下方的顶部(以便状态栏与操作栏重叠)true.之后,我应该能够在操作栏中添加一些填充,只需更改操作栏颜色也会导致颜色相同的深色状态栏,因为状态栏实际上是半透明的并且与我的操作栏重叠.但是,出于某种原因,在我的情况下不会发生这种情况.操作栏保持它是我是否设置fitsSystemWindowstruefalse或完全删除该属性.操作栏始终位于状态栏下方,当然,如果我设置透明度,则状态栏始终为黄色.

我还尝试以编程方式更改状态栏颜色时设置alpha.这确实使状态栏有点透明,但它看起来很奇怪,因为它不再是黑暗.删除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>
Run Code Online (Sandbox Code Playgroud)

这是我的XML app_bar_main:

<?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>
Run Code Online (Sandbox Code Playgroud)

Ish*_*nha 7

你的问题是因为getWindow().setStatusBarColor()不能很好地解决问题DrawerLayout.为了使您的状态栏保持半透明以及能够更改其颜色,您必须按照以下过程操作:

在您的v21/styles.xml文件中添加/修改以下主题,如下所示:

<style name="AppTheme.NoActionBar" parent="AppTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

这是标准DrawerLayout使用的主题(当然,假设您在具有该主题的活动中使用此主题DrawerLayout).如您所知,这将使您的状态栏保持半透明状态.

接下来,android:fitsSystemWindows="true"CoordinatorLayout您的中删除app_bar_main.

接下来,无论您在何处更改工具栏/状态栏的颜色,都要drawerLayout.setStatusBarBackground(colorDrawable)使用与工具栏相同的颜色(当然,假设您DrawerLayout调用了对您的引用drawerLayout).请注意,该drawerLayout.setStatusBarBackground()方法采用的Drawable对象与window.setStatusBarColor()采用int颜色的方法不同,因此您可能需要使用以下方法将颜色转换为drawable:

new ColorDrawable(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
Run Code Online (Sandbox Code Playgroud)

这将确保您的状态栏是半透明的,并使其能够更改颜色.希望你能够让这个工作.