首先,它不是一个重复的如何改变android状态栏的背景颜色
我如何改变状态栏的颜色,这应该是相同的导航栏。
我希望状态栏的颜色与导航栏的颜色相同
首先,它不是一个重复的如何改变android状态栏的背景颜色
我如何改变状态栏的颜色,这应该是相同的导航栏。
我希望状态栏的颜色与导航栏的颜色相同
当前回答
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)
其他回答
此解决方案仅适用于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)
}
在kotlin中,我能够使用以下方法解决这个问题:
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.statusBarColor = ContextCompat.getColor(this, android.R.color.transparent)
这是一个非常简单的方法来做到这一点,没有任何库: 如果操作系统版本不支持-在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));
我有这样的需求:通过编程方式改变状态栏的颜色,使其保持透明,以允许导航抽屉绘制自己与透明的状态栏重叠。
我不能用API来做这个
getWindow().setStatusBarColor(ContextCompat.getColor(activity ,R.color.my_statusbar_color)
如果你在stack overflow中检查这一行代码之前的所有人,将状态栏的透明度设置为solid with
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
我能够像这样管理状态栏的颜色和透明度:
Android 4: there's not much you can do, because you can't manage status bar color from the API ... the only thing you can do is to set the status bar as translucent and move a colored element of you UI under the status bar. To do this you need to play with android:fitsSystemWindows="false" in your main layout. This allows you to draw you layout under the status bar. Then you need to play with some padding with the top of your main layout. Android 5 and above: you have to define a style with <item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:statusBarColor">@android:color/transparent</item> this allows the navigation drawer to overlap the status bar. Then to change the color keeping the status bar transparent you have to set the status bar color with drawerLayout.setStatusBarBackgroundColor(ContextCompat.getColor(activity, R.color.my_statusbar_color)) where drawerLayout is defined like this <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true">
还有一个解决方案:
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);