首先,它不是一个重复的如何改变android状态栏的背景颜色
我如何改变状态栏的颜色,这应该是相同的导航栏。
我希望状态栏的颜色与导航栏的颜色相同
首先,它不是一个重复的如何改变android状态栏的背景颜色
我如何改变状态栏的颜色,这应该是相同的导航栏。
我希望状态栏的颜色与导航栏的颜色相同
当前回答
Android 5.0棒棒糖引入了材质设计主题,根据主题的colorPrimaryDark值自动为状态栏上色。
realdognose注意:在材质设计库中,它将是colorPrimaryVariant
由于从版本21开始的support-v7-appcompat库,设备pre-lollipop支持这一点。Chris Banes关于支持appcompat v21的博文
在官方Android开发者网站上阅读更多关于材质主题的信息
其他回答
Android 5.0棒棒糖引入了材质设计主题,根据主题的colorPrimaryDark值自动为状态栏上色。
realdognose注意:在材质设计库中,它将是colorPrimaryVariant
由于从版本21开始的support-v7-appcompat库,设备pre-lollipop支持这一点。Chris Banes关于支持appcompat v21的博文
在官方Android开发者网站上阅读更多关于材质主题的信息
一个非常非常古老的问题。但是对于那些想要改变状态栏颜色从ANDROID 5.0, API 21及以上根据主题暗和光甚至设备默认。 把这段代码放在你的活动super.onCreate(savedInstanceState)之后;setContentView(r.b ayout.activity_main);
int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
// Night mode is not active on device
// For WHITE status bar Icons color to dark
Window window = getWindow();
View view = window.getDecorView();
new WindowInsetsControllerCompat(window, view).setAppearanceLightStatusBars(true);
break;
case Configuration.UI_MODE_NIGHT_YES:
// Night mode is active on device
break;
}
也符合你的风格。Xmlput这行 <项目名称= " android: statusBarColor " > @color / colorWhite > < /项目
此解决方案仅适用于API >= 23。 在API级别30中,setSystemUiVisibility()已弃用。因此你应该像下面这样使用WindowInsetsControllerCompat
fun changeColorStatusBar(color: Int = R.color.white) {
val window: Window = window
val decorView = window.decorView
val wic = WindowInsetsControllerCompat(window, decorView)
wic.isAppearanceLightStatusBars = true
// And then you can set any background color to the status bar.
window.statusBarColor = ContextCompat.getColor(this, color)
}
这是一个非常简单的方法来做到这一点,没有任何库: 如果操作系统版本不支持-在kitkat -所以什么都不会发生。 我做这些步骤:
在我的xml中,我添加到顶部这个视图:
<视图 android: id =“@ + id / statusBarBackground” android: 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));
在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>