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

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

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

我可以使用以下行:

<item name="android:windowTranslucentStatus">true</item>

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

现在,我跟随this教程和thisthis堆栈溢出问题,但无法解决问题.所有这些文章都说,一旦我将windowTranslucentStatus设置为true,ActionBar将移动到状态栏下方的顶部(以便状态栏与操作栏重叠).之后,我应该能够在操作栏中添加一些填充,只需更改操作栏颜色也会导致颜色相同的深色状态栏,因为状态栏实际上是半透明的并且与我的操作栏重叠.但是,出于某种原因,在我的情况下不会发生这种情况.无论我将fitsSystemWindows设置为true还是false,或者完全删除属性,操作栏都会保留在原来的位置.操作栏始终位于状态栏下方,当然,如果我设置透明度,则该状态栏始终为黄色.

我还尝试以编程方式更改状态栏颜色时设置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>

这是我的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>

解决方法

您的问题是由于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>

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

接下来,从app_bar_main中的CoordinatorLayout中删除android:fitsSystemWindows =“true”.

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

new ColorDrawable(ContextCompat.getColor(getApplicationContext(),R.color.colorPrimary))

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...