要更改状态栏的颜色,请前往res/values-v21/styles.xml
并更改状态栏的颜色。
<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">#0000FF</item>
</style>
</resources>
我有这个需求:通过编程改变状态栏颜色并保持其透明,以允许导航抽屉在其上绘制自身,覆盖透明状态栏。
我无法使用API实现此功能。
getWindow().setStatusBarColor(ContextCompat.getColor(activity ,R.color.my_statusbar_color)
如果您在Stack Overflow中查看此行代码之前的内容,每个人都将状态栏的透明度设置为不透明。
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
我可以像这样管理状态栏的颜色和透明度:
在Android 4上,由于您无法从API中管理状态栏颜色,所以您无法做太多事情...您唯一能做的是将状态栏设置为半透明,并将UI的有色元素移动到状态栏下面。要做到这一点,您需要调整:
android:fitsSystemWindows="false"
在你的主布局中添加一个属性,使得你可以在状态栏下绘制布局。然后你需要在主布局顶部进行一些padding的调整。
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">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
也可以在我的设备上运行。无论如何,在经过数天的搜索后,我终于找到了一些真正有效的方法(目前仅在Lollipop上测试)。谢谢。 - DenisGL如果您想以编程方式更改状态栏颜色(并且设备已经安装了Android 5.0),那么这是一种从任何活动更改 statusBarColor 的简单方法,并且在不同的片段具有不同状态栏颜色时非常容易实现。
/**
* @param colorId id of color
* @param isStatusBarFontDark Light or Dark color
*/
fun updateStatusBarColor(@ColorRes colorId: Int, isStatusBarFontDark: Boolean = true) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val window = window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
window.statusBarColor = ContextCompat.getColor(this, colorId)
setSystemBarTheme(isStatusBarFontDark)
}
}
/** Changes the System Bar Theme. */
@RequiresApi(api = Build.VERSION_CODES.M)
private fun setSystemBarTheme(isStatusBarFontDark: Boolean) {
// Fetch the current flags.
val lFlags = window.decorView.systemUiVisibility
// Update the SystemUiVisibility depending on whether we want a Light or Dark theme.
window.decorView.systemUiVisibility = if (isStatusBarFontDark) lFlags and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv() else lFlags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
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方法中设置导航栏的margin或offset。
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);
}
编辑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>
只需将以下内容添加到styles.xml文件中即可
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- This is used for statusbar color. -->
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<!-- This is used for statusbar content color. If statusbarColor is light, use "true" otherwise use "false"-->
<item name="android:windowLightStatusBar">false</item>
</style>
<item name="android:windowLightStatusBar">false</item>
<item name="android:statusBarColor">@color/black</item>
这是我在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));
}
}
在res/values/styles.xml文件中,将colorPrimaryDark更改为所需的颜色。
<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>
Java: 在Activity的onCreate方法中使用此代码。
Window window = this.getWindow();
window.setStatusBarColor(this.getResources().getColor(R.color.main_screen_bg_color));
Kotlin:
window.statusBarColor = ContextCompat.getColor(this, R.color.colorName)
android:statusBarColor
的主题属性。你可以简单地将其添加到values-v21/styles.xml
中。 - Levite