首先,它不是一个重复的如何改变android状态栏的背景颜色
我如何改变状态栏的颜色,这应该是相同的导航栏。
我希望状态栏的颜色与导航栏的颜色相同
首先,它不是一个重复的如何改变android状态栏的背景颜色
我如何改变状态栏的颜色,这应该是相同的导航栏。
我希望状态栏的颜色与导航栏的颜色相同
更新:
棒棒糖:
public abstract void setStatusBarColor (int color)
在API级别21中添加
Android Lollipop能够改变应用程序中状态栏的颜色,以获得更沉浸式的用户体验,并与谷歌的材质设计指南相一致。
下面是如何使用新窗口更改状态栏的颜色。在API级别21中引入的setStatusBarColor方法。
改变状态栏的颜色还需要在窗口上设置两个额外的标志;你需要添加flag_draws_system_bar_background标志并清除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)
例如:material-design-everywhere
Chris Banes博客- appcompat v21:前棒棒糖设备的材料设计!
视图背景的transitionName将是android:status:background。
Android 5.0棒棒糖引入了材质设计主题,根据主题的colorPrimaryDark值自动为状态栏上色。
realdognose注意:在材质设计库中,它将是colorPrimaryVariant
由于从版本21开始的support-v7-appcompat库,设备pre-lollipop支持这一点。Chris Banes关于支持appcompat v21的博文
在官方Android开发者网站上阅读更多关于材质主题的信息
这是一个非常简单的方法来做到这一点,没有任何库: 如果操作系统版本不支持-在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));
放置这是你的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">@color/color_primary</item>
</style>
</resources>
好吧,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">
注意height和elevation属性,这些设置在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>
对于工具栏,添加top margin属性:
<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样式-v19.xml和样式-v21.xml中,添加window半透明attr:
styles-v19.xml v21:
<resources>
<item name="android:windowTranslucentStatus">true</item>
</resources>
最后,在你的维度上,dimensions -v19, dimensions -v21,添加工具栏topMargin的值,以及statusBarHeight的高度: 小于KitKat的:
<resources>
<dimen name="toolbarElevation">4dp</dimen>
<dimen name="appBarTopMargin">0dp</dimen>
<dimen name="statusBarHeight">0dp</dimen>
</resources>
状态栏的高度总是24dp 维-v19.xml的奇巧和以上:
<resources>
<dimen name="statusBarHeight">24dp</dimen>
<dimen name="appBarTopMargin">24dp</dimen>
</resources>
如果需要,只需要添加抬高:
<resources>
<dimen name="statusBarElevation">4dp</dimen>
</resources>
这是Jellybean KitKat和Lollipop的结果:
还有一个解决方案:
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);
这就是我在奇巧(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”:
<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>
如果你想在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);
}
然后状态栏将是这样的。
我有这样的需求:通过编程方式改变状态栏的颜色,使其保持透明,以允许导航抽屉绘制自己与透明的状态栏重叠。
我不能用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 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));
}
}
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)
在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>
你可以使用这段简单的代码:
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));
}
将colorPrimaryDark更改为您想要的颜色到res/values/styles.xml文件中
<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>
要改变上面棒棒糖的颜色,只需将其添加到您的styles.xml中
<item name="android:statusBarColor">@color/statusBarColor</item>
但是请记住,如果你想让状态栏有一个浅色,也可以添加这一行
<item name="android:windowLightStatusBar">true</item>
若要更改状态栏的颜色,请转到 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>
如果你想通过编程方式改变状态栏的颜色(前提是设备有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
}
解决方法很简单,把下面的代码放到style.xml中
对于黑暗模式:
<item name="android:windowLightStatusBar">false</item>
<item name="android:statusBarColor">@color/black</item>
我使用这段代码将状态栏更改为透明
activity?.window?.setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
)
要将其更改为样式中的颜色,请使用以下代码 我在onDetach()中的fragment中使用了
activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
如果要设置自定义可绘制文件,请使用此代码段
fun setCustomStatusBar(){
if (Build.VERSION.SDK_INT >= 21) {
val decor = window.decorView
decor.viewTreeObserver.addOnPreDrawListener(object :
ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
decor.viewTreeObserver.removeOnPreDrawListener(this)
val statusBar = decor.findViewById<View>
(android.R.id.statusBarBackground)
statusBar.setBackgroundResource(R.drawable.bg_statusbar)
return true
}
})
}
}
只需在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>
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorPrimaryVariant">@color/colorPrimaryDark</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
注意:设置colorPrimaryVariant
从要更改状态栏颜色的活动调用方法。
blackIconStatusBar(this, R.color.white);
方法定义
public static void blackIconStatusBar(Activity activity, int color) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, color));
}
一个非常非常古老的问题。但是对于那些想要改变状态栏颜色从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 > < /项目
Java: 在活动的onCreate方法中使用此方法
Window window = this.getWindow();
window.setStatusBarColor(this.getResources().getColor(R.color.main_screen_bg_color));
科特林:
window.statusBarColor = ContextCompat.getColor(this, R.color.colorName)
在values/theme.xml中,添加命名为name="android:statusBarColor"的项。
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.YourAppName" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
...
...
...
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">@color/purple_700</item>
</style>
</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)
}
在kotlin中,我能够使用以下方法解决这个问题:
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.statusBarColor = ContextCompat.getColor(this, android.R.color.transparent)