Android 5.0 Lollipop引入了Material Design主题,该主题根据主题中colorPrimaryDark
值自动着色状态栏。
注意:使用Material Design库时,应该使用
colorPrimaryVariant
。
由于支持库support-v7-appcompat从版本21开始提供支持,因此这也支持在Lollipop之前的设备上实现。 Chris Banes的博客文章介绍了有关支持appcompat v21的更多信息
themes.xml
中,将使用 colorPrimaryVariant
替代 colorPrimaryDark
。 - Reejesh PK更新:
棒棒糖(Lollipop):
public abstract void setStatusBarColor (int color)
自 API level 21 起引入
安卓棒棒糖带来了改变应用程序状态栏颜色的功能,以便更深入的用户体验,并符合 Google 的 Material Design Guidelines
。
以下是示例代码,演示了如何使用新的 window.setStatusBarColor
方法来改变状态栏颜色,此方法在API level 21
中被引入。
同时, 更改状态栏颜色还需要在窗口上设置两个附加标志; 需要添加 FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
并清除 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)
Chris Banes博客- appcompat v21:面向pre-Lollipop设备的材料设计!
视图背景的transitionName
将为android:status:background
。
values-v21/styles.xml
文件中设置android:statusBarColor
来配置状态栏颜色,详见http://www.androiddocs.com/training/material/theme.html,示例如下:<item name="android:statusBarColor">@color/primary_color</item> - JL Westvalues-v21/styles.xml
中,以在Lollipop上启用此功能。<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">@color/color_primary</item>
</style>
</resources>
针对Java开发者:
如@Niels所说,您需要将其放置在values-v21/styles.xml中:
<item name="android:statusBarColor">@color/black</item>
如果您想要一个单独的styles.xml文件,请在tools:targetApi="lollipop"
中添加:
<item name="android:statusBarColor" tools:targetApi="lollipop">@color/black</item>
针对 Kotlin 开发者:
window.statusBarColor = ContextCompat.getColor(this, R.color.color_name)
这是一种非常简便的方法,可以在没有任何库的情况下完成此操作: 如果操作系统版本不受支持-在4.4以下-则不会发生任何事情。 我执行以下步骤:
<View
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);
}
}
同时您需要使用这两种方法来获取ActionBar和状态栏的高度:
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));
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));
}
ContextCompat
进一步简化此过程。在Kotlin中,只需这样做:window.statusBarColor = ContextCompat.getColor(this, R.color.colorName)
- sunadorer要更改上面棒棒糖的颜色,只需将以下内容添加到您的styles.xml文件中
<item name="android:statusBarColor">@color/statusBarColor</item>
但请记住,如果您想为状态栏使用浅色,请也添加这一行。
<item name="android:windowLightStatusBar">true</item>
嗯,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">
注意高度和高程属性,这些将在下面的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>
为工具栏添加顶部边距属性:
<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 style-v19.xml和styles-v21.xml中,添加windowTranslucent属性:
styles-v19.xml,v21:
<resources>
<item name="android:windowTranslucentStatus">true</item>
</resources>
最后,在你的dimens、dimens-v19和dimens-v21文件中,添加Toolbar的topMargin值以及statusBarHeight的高度: 低于KitKat版本的dimens.xml:
<resources>
<dimen name="toolbarElevation">4dp</dimen>
<dimen name="appBarTopMargin">0dp</dimen>
<dimen name="statusBarHeight">0dp</dimen>
</resources>
状态栏的高度始终为 24dp 在 Android 4.4 KitKat 及以上版本中使用 dimens-v19.xml:
<resources>
<dimen name="statusBarHeight">24dp</dimen>
<dimen name="appBarTopMargin">24dp</dimen>
</resources>
对于Lolipop,使用dimens-v21.xml文件,如果需要,只需添加elevation即可:
<resources>
<dimen name="statusBarElevation">4dp</dimen>
</resources>
这是Jellybean、KitKat和Lollipop的结果:
只需在 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>
你可以使用此函数更改状态栏的颜色。它适用于安卓 L 版本(API 21及以上),需要一个颜色字符串,例如:"#ffffff"
。
private void changeStatusBarColor(String color){
if (Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor(color));
}
}
android:statusBarColor
的主题属性。你可以简单地将其添加到values-v21/styles.xml
中。 - Levite