首先,它不是一个重复的如何改变android状态栏的背景颜色
我如何改变状态栏的颜色,这应该是相同的导航栏。
我希望状态栏的颜色与导航栏的颜色相同
首先,它不是一个重复的如何改变android状态栏的背景颜色
我如何改变状态栏的颜色,这应该是相同的导航栏。
我希望状态栏的颜色与导航栏的颜色相同
当前回答
你可以使用这段简单的代码:
Kotlin中的一行代码:
window.statusBarColor = ContextCompat.getColor(this, R.color.colorName)
原始答案与Java和手动版本检查:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindow().setStatusBarColor(getResources().getColor(R.color.colorAccentDark_light, this.getTheme()));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(getResources().getColor(R.color.colorAccentDark_light));
}
其他回答
更新:
棒棒糖:
public abstract void setStatusBarColor (int color)
在API级别21中添加
Android Lollipop能够改变应用程序中状态栏的颜色,以获得更沉浸式的用户体验,并与谷歌的材质设计指南相一致。
下面是如何使用新窗口更改状态栏的颜色。在API级别21中引入的setStatusBarColor方法。
改变状态栏的颜色还需要在窗口上设置两个额外的标志;你需要添加flag_draws_system_bar_background标志并清除FLAG_TRANSLUCENT_STATUS标志。
工作代码:
import android.view.Window;
...
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博客- appcompat v21:前棒棒糖设备的材料设计!
视图背景的transitionName将是android:status:background。
好吧,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">
注意height和elevation属性,这些设置在values中,values-v19, values-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>
对于工具栏,添加top margin属性:
<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">
< / android.support.v7.widget.Toolbar >
在你的appTheme样式-v19.xml和样式-v21.xml中,添加window半透明attr:
styles-v19.xml v21:
<resources>
<item name="android:windowTranslucentStatus">true</item>
</resources>
最后,在你的维度上,dimensions -v19, dimensions -v21,添加工具栏topMargin的值,以及statusBarHeight的高度: 小于KitKat的:
<resources>
<dimen name="toolbarElevation">4dp</dimen>
<dimen name="appBarTopMargin">0dp</dimen>
<dimen name="statusBarHeight">0dp</dimen>
</resources>
状态栏的高度总是24dp 维-v19.xml的奇巧和以上:
<resources>
<dimen name="statusBarHeight">24dp</dimen>
<dimen name="appBarTopMargin">24dp</dimen>
</resources>
如果需要,只需要添加抬高:
<resources>
<dimen name="statusBarElevation">4dp</dimen>
</resources>
这是Jellybean KitKat和Lollipop的结果:
从要更改状态栏颜色的活动调用方法。
blackIconStatusBar(this, R.color.white);
方法定义
public static void blackIconStatusBar(Activity activity, int color) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, color));
}
Java开发人员:
正如@Niels所说,你必须在values-v21/styles.xml中放置:
<item name="android:statusBarColor">@color/black</item>
但是如果你想要单个样式。xml,需要添加工具:targetApi="lollipop",比如:
<item name="android:statusBarColor" tools:targetApi="lollipop">@color/black</item>
对于Kotlin开发者:
window.statusBarColor = ContextCompat.getColor(this, R.color.color_name)
将colorPrimaryDark更改为您想要的颜色到res/values/styles.xml文件中
<resources>
<color name="colorPrimary">#800000</color>
<color name="colorPrimaryDark">#303F9F</color> //This Line
<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>
</resources>