首先,它不是一个重复的如何改变android状态栏的背景颜色
我如何改变状态栏的颜色,这应该是相同的导航栏。
我希望状态栏的颜色与导航栏的颜色相同
首先,它不是一个重复的如何改变android状态栏的背景颜色
我如何改变状态栏的颜色,这应该是相同的导航栏。
我希望状态栏的颜色与导航栏的颜色相同
当前回答
只需在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>
其他回答
此解决方案仅适用于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)
}
要改变上面棒棒糖的颜色,只需将其添加到您的styles.xml中
<item name="android:statusBarColor">@color/statusBarColor</item>
但是请记住,如果你想让状态栏有一个浅色,也可以添加这一行
<item name="android:windowLightStatusBar">true</item>
我有这样的需求:通过编程方式改变状态栏的颜色,使其保持透明,以允许导航抽屉绘制自己与透明的状态栏重叠。
我不能用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">
一个非常非常古老的问题。但是对于那些想要改变状态栏颜色从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 > < /项目
如果你想在Android 4.4及以上版本上工作,试试这个。我参考了Harpreet的答案和这个链接。Android和透明的状态栏
首先,在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;
}
之前:
后:
状态栏的颜色已经改变了,但是导航栏被切断了,所以我们需要在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);
}
然后状态栏将是这样的。