在Android中具有动态ActionBar颜色的半透明状态栏 [英] Translucent StatusBar with dynamic ActionBar color in Android

查看:298
本文介绍了在Android中具有动态ActionBar颜色的半透明状态栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个半透明的状态栏(使我的导航视图 BEHIND 状态栏),但仍然想要动态改变我的动作栏的颜色。因此,状态栏颜色需要更改为我的操作栏颜色的较深版本。



如果我将状态栏设置为透明,就像许多来源建议的一样,我的primary_dark颜色用作状态栏的背景。但是,由于我将在运行时更改操作栏颜色,primary_dark不一定是我的操作栏的深色。



如果我将状态栏设置为操作栏颜色,透明度消失了。如果我将状态栏设置为操作栏颜色并添加透明度,状态栏看起来既不错也不错,我重叠的导航视图仍然不是很透明/多彩。



Google收件箱有三种不同的颜色:收件箱(蓝色),已延后(黄色)和完成(绿色)。



实现这个行为?

解决方案

实际上,它很容易实现。





第一步 - 改变工具栏的高度




  • 更改 height wrap_content



因此,我看起来像这样:

 < android.support.v7.widget.Toolbar 
android:id =@ + id / toolbar
android:layout_width =match_parent
android:layout_height =wrap_content
android:background =?attr / colorPrimary
app:popupTheme =@ style / AppTheme.PopupOverlay/>

然后覆盖 v19

 <?xml version =1.0encoding =utf-8?& 
< resources>
< style name =AppThemeparent =Theme.AppCompat.Light.DarkActionBar>
<! - 样式属性 - >
....
<! - 这些属性很重要: - >
< item name =android:windowTranslucentStatus> true< / item>
< item name =windowActionBarOverlay> false< / item>
< item name =android:windowActionBarOverlay> false< / item>
< item name =android:fitsSystemWindows> false< / item>
< / style>
< / resources>然后,在 Activity 中,将 $ <$> $ <$> $ <$> code> :

  c $ c>工具栏toolbar =(工具栏)findViewById(R.id.toolbar); 
setSupportActionBar(toolbar);
toolbar.setPadding(0,getStatusBarHeight(),0,0);
......
......
public int getStatusBarHeight(){
int result = 0;

if(android.os.Build.VERSION.SDK_INT> = android.os.Build.VERSION_CODES.KITKAT){
int resourceId = getResources()。getIdentifier(status_bar_height, dimen,android);
if(resourceId> 0){
result = getResources()。getDimensionPixelSize(resourceId);
}
}
返回结果;
}

实际上,它差不多。现在,一旦我想更改工具栏的颜色,我就叫这个:

  if(getSupportActionBar {
getSupportActionBar()。setBackgroundDrawable(new ColorDrawable(Color.GREEN));
}

活动的布局:

 <?xml version =1.0encoding =utf-8?> 
< android.support.v4.widget.DrawerLayout xmlns:app =http://schemas.android.com/apk/res-auto
xmlns:tools =http:// schemas .android.com / tools
xmlns:android =http://schemas.android.com/apk/res/android
android:id =@ + id / drawer_layout
android:layout_width =match_parent
android:layout_height =match_parent
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 =false
app:headerLayout =@ layout / nav_header_main
app:menu =@ menu / activity_main_drawer/>

< /android.support.v4.widget.DrawerLayout>

注意!在Kitkat OS版本前,状态栏仍为相同,非半透明。



我已将测试应用程序的源代码上传到我的保管箱 - 请随时查看



它有助于


I'm trying to realize a translucent statusbar (so that my navigation-view is BEHIND the statusbar) but still like to change the color of my actionbar dynamically. Because of this, the statusbar color needs to change to a darker version of my actionbar color.

If I set my statusbar to transparent, as many sources suggest, my primary_dark color is used as the background of my statusbar. However, as I will change the actionbar color during runtime, primary_dark must not necessarily be the dark color of my actionbar.

If I set my statusbar to the actionbar color, the transparency is gone. If I set my statusbar to the actionbar color and add transparency, the statusbar looks neither wrong nor right and my overlapping navigationview is still not very 'transparent' / 'colorful'.

Google Inbox has three separate colors: Inbox (blue), Snoozed (yellow) and Done (green).

What can I do to achieve this behaviour?

解决方案

Actually, it's fairly easy to implement.

First step - is to change height of the Toolbar:

  • change height to wrap_content

so for me it looks like this:

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

Then you override resources for v19:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
            <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
                    <!-- Style properties -->
                    ....
                    <!-- These properties are important:-->
                    <item name="android:windowTranslucentStatus">true</item>
                    <item name="windowActionBarOverlay">false</item>
                    <item name="android:windowActionBarOverlay">false</item>
                    <item name="android:fitsSystemWindows">false</item>
            </style>
    </resources>

Then, in the Activity, setting padding for the Toolbar:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
    ......
    ......
    public int getStatusBarHeight() {
        int result = 0;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = getResources().getDimensionPixelSize(resourceId);
            }
        }
        return result;
    }

And actually, it's pretty much it. Now once I want to change colour of the Toolbar, I'm calling this:

    if (getSupportActionBar() != null) {
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.GREEN));
    }

The activity's layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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="false"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

NB! On pre-Kitkat OS-versions, status bar remained the same, non-translucent.

I've uploaded the source code of the test-application to my dropbox - feel free to check it out.

I hope, it helps

这篇关于在Android中具有动态ActionBar颜色的半透明状态栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆