首页 文章

如何更改android中的状态栏颜色

提问于
浏览
296

首先,它不像How to change the background color of android status bar那样重复

如何更改状态栏颜色,该颜色应与导航栏中的颜色相同 .

我希望状态栏颜色与导航栏颜色相同

enter image description here

13 回答

  • 165

    Android 5.0 Lollipop引入了Material Design主题,它根据主题的 colorPrimaryDark 值自动为状态栏添加颜色 .

    由于从版本21开始的库支持-v7-appcompat,这在设备pre-lollipop上受支持.Blogpost about support appcompat v21 from Chris Banes

    enter image description here

    Read more about the Material Theme on the official Android Developers website

  • 3

    更新:

    棒糖:

    public abstract void setStatusBarColor (int color)
    

    在API级别21中添加

    Android Lollipop带来了改变应用中状态栏颜色的能力,以获得更加身临其境的用户体验,并与Google的 Material Design Guidelines 保持一致 .

    以下是使用 API level 21 中引入的新 window.setStatusBarColor 方法更改状态栏颜色的方法 .

    更改状态栏的颜色还需要在窗口上设置两个额外的标志;您需要添加 FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS 标志并清除 FLAG_TRANSLUCENT_STATUS 标志 .

    工作守则:

    Window window = activity.getWindow();
    
    // clear FLAG_TRANSLUCENT_STATUS flag:
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    
    // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    
    // finally change the color
    window.setStatusBarColor(ContextCompat.getColor(activity,R.color.my_statusbar_color));
    

    官方开发人员参考:setStatusBarColor(int)

    示例:material-design-everywhere

    Chris Banes Blog- appcompat v21: material design for pre-Lollipop devices!

    enter image description here

    视图背景的 transitionName 将为 android:status:background .

  • 3

    这是你的values-v21 / styles.xml,在Lollipop上启用它:

    <resources>
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light">
            <item name="colorPrimary">@color/color_primary</item>
            <item name="colorPrimaryDark">@color/color_secondary</item>
            <item name="colorAccent">@color/color_accent</item>
            <item name="android:statusBarColor">@color/color_primary</item>
        </style>
    </resources>
    
  • 2

    没有任何库,这是非常简单的方法:如果不支持操作系统版本 - 在kitkat下 - 所以没有任何事情发生 . 我这样做了:

    • 在我的xml中我添加到顶部此视图:

    <查看
    android:id =“@ id / statusBarBackground”
    机器人:layout_width = “match_parent”
    android:layout_height =“wrap_content”/>

    然后我做了这个方法:

    public void setStatusBarColor(View statusBar,int color){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
               Window w = getWindow();
               w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
               //status bar height
               int actionBarHeight = getActionBarHeight();
               int statusBarHeight = getStatusBarHeight();
               //action bar height
               statusBar.getLayoutParams().height = actionBarHeight + statusBarHeight;
               statusBar.setBackgroundColor(color);
         }
    }
    

    你需要这两种方法来获取动作栏和状态栏高度:

    public int getActionBarHeight() {
        int actionBarHeight = 0;
        TypedValue tv = new TypedValue();
        if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
        {
           actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
        }
        return actionBarHeight;
    }
    
    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
    

    那么你唯一需要的是设置状态栏颜色的这一行:

    setStatusBarColor(findViewById(R.id.statusBarBackground),getResources().getColor(android.R.color.white));
    
  • 16

    好吧,Izhar解决方案还可以,但是,就我个人而言,我试图避免代码看起来像这样:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    //Do what you need for this SDK
    };
    

    同样,我也不喜欢重复代码 . 在你的回答中,我必须在所有活动中添加这样的代码行:

    setStatusBarColor(findViewById(R.id.statusBarBackground),getResources().getColor(android.R.color.white));
    

    因此,我使用了Izhar解决方案并使用XML来获得相同的结果:为StatusBar status_bar.xml创建一个布局

    <View xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="@dimen/statusBarHeight"
         android:background="@color/primaryColorDark"
         android:elevation="@dimen/statusBarElevation">
    

    注意高度和高程属性,这些属性将设置为值,值-v19,值-v21进一步向下 .

    使用include,main_activity.xml将此布局添加到您的活动布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/Black" >
    
    <include layout="@layout/status_bar"/>
    <include android:id="@+id/app_bar" layout="@layout/app_bar"/>
    //The rest of your layout       
    </RelativeLayout>
    

    对于工具栏,添加上边距属性:

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:background="@color/primaryColor"
    app:theme="@style/MyCustomToolBarTheme"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    android:elevation="@dimen/toolbarElevation"
    android:layout_marginTop="@dimen/appBarTopMargin"
    android:textDirection="ltr"
    android:layoutDirection="ltr">
    

    在appTheme style-v19.xml和styles-v21.xml中,添加windowTranslucent attr:

    styles-v19.xml,v21:

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

    最后,在您的维度上,dimens-v19,dimens-v21,添加工具栏topMargin的值,以及statusBarHeight:dimens.xml的高度,小于KitKat:

    <resources>
    <dimen name="toolbarElevation">4dp</dimen>
    <dimen name="appBarTopMargin">0dp</dimen>
    <dimen name="statusBarHeight">0dp</dimen>
    </resources>
    

    KitKat及以上版本的状态栏高度始终为24dp dimens-v19.xml:

    <resources>
    <dimen name="statusBarHeight">24dp</dimen>
    <dimen name="appBarTopMargin">24dp</dimen>
    </resources>
    

    维持Lolipop的dimens-v21.xml,如果需要,只需添加标高:

    <resources>
    <dimen name="statusBarElevation">4dp</dimen>
    </resources>
    

    这是Jellybean KitKat和Lollipop的结果:

    enter image description here

  • 29

    只需在 res/values/styles.xml 中创建一个新主题,您可以在其中更改状态栏颜色"colorPrimaryDark":

    <style name="AppTheme.GrayStatusBar" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimaryDark">@color/colorGray</item>
    </style>
    

    并将 AndroidManifest.xml 中的活动主题修改为您想要的活动主题,在下一个活动中,您可以通过选择原始主题将颜色更改回原始主题:

    <activity
        android:name=".LoginActivity"
        android:theme="@style/AppTheme.GrayStatusBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    

    这就是你的 res/values/colors.xml 应该是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="colorPrimary">#3F51B5</color>
        <color name="colorPrimaryDark">#303F9F</color>
        <color name="colorAccent">#c6d6f0</color>
        <color name="colorGray">#757575</color>
    </resources>
    
  • 279

    我有这个要求: Changing programmatically the status bar color keeping it transparent ,允许导航抽屉绘制自己与trasparent状态栏重叠 .

    我不能使用API来做到这一点

    getWindow().setStatusBarColor(ContextCompat.getColor(activity ,R.color.my_statusbar_color)
    

    如果你在这里检查堆栈溢出,那么在该行代码之前将状态栏的透明度设置为solid

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    

    我能够像这样管理 color and transparency 状态栏:

    • Android 4:你无能为力,因为你无法通过API管理状态栏颜色...你唯一能做的就是将状态栏设置为半透明并移动你的UI的彩色元素状态栏 . 要做到这一点,你需要玩
    android:fitsSystemWindows="false"
    

    在你的主要布局中 . 这允许您在状态栏下绘制布局 . 然后你需要在主布局的顶部玩一些填充 .

    • Android 5及以上版本:你必须定义一个样式
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    

    这允许导航抽屉与状态栏重叠 .

    然后要更改保持状态栏透明的颜色,您必须使用设置状态栏颜色

    drawerLayout.setStatusBarBackgroundColor(ContextCompat.getColor(activity, R.color.my_statusbar_color))
    

    其中drawerLayout的定义如下

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
  • 12

    正如@Niels所说,你必须放在values-v21 / styles.xml中:

    <item name="android:statusBarColor">@color/black</item>
    

    但如果你想要单个styles.xml,请添加 tools:targetApi="lollipop" ,如:

    <item name="android:statusBarColor" tools:targetApi="lollipop">@color/black</item>
    
  • 5

    您可以使用此功能更改状态栏颜色 . 在Android L上工作意味着API 21及更高版本,需要一个颜色字符串,如 "#ffffff" .

    private void changeStatusBarColor(String color){
        if (Build.VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.parseColor(color));
        }
    }
    
  • 47

    如果您想使用Android 4.4及更高版本,请尝试此操作 . 我指的是Harpreet的答案和这个链接 . Android and the transparent status bar

    首先,在Activity的onCreate方法中调用setStatusBarColored方法(我把它放在一个util类中) . 我在这里使用图像,您可以将其更改为使用颜色 .

    public static void setStatusBarColored(Activity context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            Window w = context.getWindow();
            w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            int statusBarHeight = getStatusBarHeight(context);
    
            View view = new View(context);
            view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            view.getLayoutParams().height = statusBarHeight;
            ((ViewGroup) w.getDecorView()).addView(view);
            view.setBackground(context.getResources().getDrawable(R.drawable.navibg));
        }
    }
    
    public static int getStatusBarHeight(Activity context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
    

    之前:
    before

    之后:
    after

    状态栏的颜色已更改,但导航栏已被切断,因此我们需要在onCreate方法中设置导航栏的边距或偏移量 .

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, (int)(this.getResources().getDimension(R.dimen.navibar_height)));
            layoutParams.setMargins(0, Utils.getStatusBarHeight(this), 0, 0);
    
            this.findViewById(R.id.linear_navi).setLayoutParams(layoutParams);
        }
    

    然后状态栏将如下所示 .

    status bar

  • 11

    这对KitKat来说非常有用,效果很好 .

    public static void setTaskBarColored(Activity context) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            {
                Window w = context.getWindow();
                w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                //status bar height
                int statusBarHeight = Utilities.getStatusBarHeight(context);
    
                View view = new View(context);
                view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                view.getLayoutParams().height = statusBarHeight;
                ((ViewGroup) w.getDecorView()).addView(view);
                view.setBackgroundColor(context.getResources().getColor(R.color.colorPrimaryTaskBar));
            }
        }
    
  • 7

    将Values中colors.xml中的colorPrimary编辑为您希望状态栏的颜色 . 例如:

    <resources>
    <color name="colorPrimary">#800000</color> // changes the status bar color to Burgundy
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="red">#FF0000</color>
    <color name="white">#FFFFFF</color>
    <color name="cream">#fffdd0</color>
    <color name="burgundy">#800000</color>
    
  • 462

    还有一个解决方案

    final View decorView = w.getDecorView();
    View view = new View(BaseControllerActivity.this);
    final int statusBarHeight = UiUtil.getStatusBarHeight(ContextHolder.get());
    view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight));
    view.setBackgroundColor(colorValue);
    ((ViewGroup)decorView).addView(view);
    

相关问题