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));
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;
}
<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>
462
还有一个解决方案
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);
13 回答
Android 5.0 Lollipop引入了Material Design主题,它根据主题的
colorPrimaryDark
值自动为状态栏添加颜色 .由于从版本21开始的库支持-v7-appcompat,这在设备pre-lollipop上受支持.Blogpost about support appcompat v21 from Chris Banes
Read more about the Material Theme on the official Android Developers website
更新:
棒糖:
在API级别21中添加
Android Lollipop带来了改变应用中状态栏颜色的能力,以获得更加身临其境的用户体验,并与Google的
Material Design Guidelines
保持一致 .以下是使用 API level 21 中引入的新
window.setStatusBarColor
方法更改状态栏颜色的方法 .更改状态栏的颜色还需要在窗口上设置两个额外的标志;您需要添加
FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
标志并清除FLAG_TRANSLUCENT_STATUS
标志 .工作守则:
官方开发人员参考:setStatusBarColor(int)
示例:material-design-everywhere
Chris Banes Blog- appcompat v21: material design for pre-Lollipop devices!
视图背景的
transitionName
将为android:status:background
.这是你的values-v21 / styles.xml,在Lollipop上启用它:
没有任何库,这是非常简单的方法:如果不支持操作系统版本 - 在kitkat下 - 所以没有任何事情发生 . 我这样做了:
然后我做了这个方法:
你需要这两种方法来获取动作栏和状态栏高度:
那么你唯一需要的是设置状态栏颜色的这一行:
好吧,Izhar解决方案还可以,但是,就我个人而言,我试图避免代码看起来像这样:
同样,我也不喜欢重复代码 . 在你的回答中,我必须在所有活动中添加这样的代码行:
因此,我使用了Izhar解决方案并使用XML来获得相同的结果:为StatusBar status_bar.xml创建一个布局
注意高度和高程属性,这些属性将设置为值,值-v19,值-v21进一步向下 .
使用include,main_activity.xml将此布局添加到您的活动布局:
对于工具栏,添加上边距属性:
在appTheme style-v19.xml和styles-v21.xml中,添加windowTranslucent attr:
styles-v19.xml,v21:
最后,在您的维度上,dimens-v19,dimens-v21,添加工具栏topMargin的值,以及statusBarHeight:dimens.xml的高度,小于KitKat:
KitKat及以上版本的状态栏高度始终为24dp dimens-v19.xml:
维持Lolipop的dimens-v21.xml,如果需要,只需添加标高:
这是Jellybean KitKat和Lollipop的结果:
只需在 res/values/styles.xml 中创建一个新主题,您可以在其中更改状态栏颜色"colorPrimaryDark":
并将 AndroidManifest.xml 中的活动主题修改为您想要的活动主题,在下一个活动中,您可以通过选择原始主题将颜色更改回原始主题:
这就是你的 res/values/colors.xml 应该是这样的:
我有这个要求: Changing programmatically the status bar color keeping it transparent ,允许导航抽屉绘制自己与trasparent状态栏重叠 .
我不能使用API来做到这一点
如果你在这里检查堆栈溢出,那么在该行代码之前将状态栏的透明度设置为solid
我能够像这样管理 color and transparency 状态栏:
在你的主要布局中 . 这允许您在状态栏下绘制布局 . 然后你需要在主布局的顶部玩一些填充 .
这允许导航抽屉与状态栏重叠 .
然后要更改保持状态栏透明的颜色,您必须使用设置状态栏颜色
其中drawerLayout的定义如下
正如@Niels所说,你必须放在values-v21 / styles.xml中:
但如果你想要单个styles.xml,请添加
tools:targetApi="lollipop"
,如:您可以使用此功能更改状态栏颜色 . 在Android L上工作意味着API 21及更高版本,需要一个颜色字符串,如
"#ffffff"
.如果您想使用Android 4.4及更高版本,请尝试此操作 . 我指的是Harpreet的答案和这个链接 . Android and the transparent status bar
首先,在Activity的onCreate方法中调用setStatusBarColored方法(我把它放在一个util类中) . 我在这里使用图像,您可以将其更改为使用颜色 .
之前:
之后:
状态栏的颜色已更改,但导航栏已被切断,因此我们需要在onCreate方法中设置导航栏的边距或偏移量 .
然后状态栏将如下所示 .
这对KitKat来说非常有用,效果很好 .
将Values中colors.xml中的colorPrimary编辑为您希望状态栏的颜色 . 例如:
还有一个解决方案