怎么改变状态栏的颜色安卓状态栏的背景颜色

Andorid_API19设置状态栏颜色_Android开发_动态网站制作指南
Andorid_API19设置状态栏颜色
来源:人气:88
@作者 : 西野奈留
@博客:http://blog.csdn.net/narunishino
在android 4.4的系统中改变状态栏的颜色。
1.gradle中添加依赖 compile 'com.android.support:appcompat-v7:25.2.0'
2.在Aheme中进行如下设置:
&resources&
&!-- 设置为AppCompat的NoActionBar --&
&style name="AppTheme" parent="Theme.AppCompat.NoActionBar"&
&!-- 添加android:windowTranslucentStatus属性和android:fitsSystemWindows属性 --&
&item name="android:windowTranslucentStatus"&true&/item&
&item name="android:fitsSystemWindows"&true&/item&
&item name="colorimary"&@color/colorPrimary&/item&
&item name="colorPrimaryDark"&@color/colorPrimaryDark&/item&
&/resources&
3.修改activity_main.xml为:
&LinearLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
// 1.这里的颜色就是你想要的状态栏的颜色
android:background="@color/colorPrimary"
android:orientation="vertical"
tools:context="com.nishinonaru.testdownloadprovider.MainActivity"&
// 2.要添加Toolbar
&android.support.v7.widget.Toolbar
// toolbar的id,一会要用到
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:title="@string/app_name"&
&/android.support.v7.widget.Toolbar&
&RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
// 3.这里的颜色是主界面的背景色,如果不设置的话,会显示更上面状态栏一样的颜色
android:background="#ffffff"&
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="#000000"/&
&/RelativeLayout&
&/LinearLayout&
4.在MainActivity中添加toolbar:
import android.os.B
import android.support.v7.app.AppCompatA
import android.support.v7.widget.T
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(tb);
优质网站模板Android 沉浸式改变小米魅族状态栏颜色的实例代码-android100学习网
Android 沉浸式改变小米魅族状态栏颜色的实例代码
这个是基于SystemBarTintManager更改的 增加一个方法:用于更改MIUIV6系统上的状态栏字体颜色 ,目前我仅仅只发现MIUIV6上可以更改,在android5.0上以及其它4.4...
这个是基于SystemBarTintManager更改的
增加一个方法:用于更改MIUIV6系统上的状态栏字体颜色 ,目前我仅仅只发现MIUIV6上可以更改,在android5.0上以及其它4.4以上系统没有发现可以更改字体颜色的代码
public void setStatusBarDarkMode(boolean darkmode, Activity activity) {
if (sIsMiuiV6) {
Class&? extends Window& clazz = activity.getWindow().getClass();
int darkModeFlag = 0;
Class&?& layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
} catch (Exception e) {
e.printStackTrace();
* Copyright (C) 2013 readyState Software Ltd
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
import android.annotation.SuppressL
import android.annotation.TargetA
import android.app.A
import android.content.C
import android.content.res.C
import android.content.res.R
import android.content.res.TypedA
import android.graphics.drawable.D
import android.os.B
import android.util.DisplayM
import android.util.TypedV
import android.view.G
import android.view.V
import android.view.ViewC
import android.view.ViewG
import android.view.W
import android.view.WindowM
import android.widget.FrameLayout.LayoutP
import java.lang.reflect.F
import java.lang.reflect.M
* Class to manage status and navigation bar tint effects when using KitKat
* translucent system UI modes.
public class SystemBarTintManager {
* The default system bar tint color value.
public static final int DEFAULT_TINT_COLOR = 0x;
private static String sNavBarO
private final SystemBarConfig mC
private boolean mStatusBarA
private boolean mNavBarA
private boolean mStatusBarTintE
private boolean mNavBarTintE
private View mStatusBarTintV
private View mNavBarTintV
private static boolean sIsMiuiV6;
// Android allows a system property to override the presence of the navigation bar.
// Used by the emulator.
// See /android/platform_frameworks_base/blob/master/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java#L1076
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.KITKAT) {
Class c = Class.forName("android.os.SystemProperties");
Method m = c.getDeclaredMethod("get", String.class);
m.setAccessible(true);
sIsMiuiV6 = "V6".equals((String) m.invoke(c, "ro.miui.ui.version.name"));
sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
} catch (Throwable e) {
sNavBarOverride =
* Constructor. Call this in the host activity onCreate method after its
* content view has been set. You should always create new instances when
* the host activity is recreated.
* @param activity The host activity.
@TargetApi(19)
public SystemBarTintManager(Activity activity) {
Window win = activity.getWindow();
ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.KITKAT) {
// check theme attrs
int[] attrs = {android.R.attr.windowTranslucentStatus,
android.R.attr.windowTranslucentNavigation};
TypedArray a = activity.obtainStyledAttributes(attrs);
mStatusBarAvailable = a.getBoolean(0, false);
mNavBarAvailable = a.getBoolean(1, false);
} finally {
a.recycle();
// check window flags
WindowManager.LayoutParams winParams = win.getAttributes();
int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if ((winParams.flags & bits) != 0) {
mStatusBarAvailable =
bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
if ((winParams.flags & bits) != 0) {
mNavBarAvailable =
mConfig = new SystemBarConfig(activity, mStatusBarAvailable, mNavBarAvailable);
// device might not have virtual navigation keys
if (!mConfig.hasNavigtionBar()) {
mNavBarAvailable =
if (mStatusBarAvailable) {
setupStatusBarView(activity, decorViewGroup);
if (mNavBarAvailable) {
setupNavBarView(activity, decorViewGroup);
* Enable tinting of the system status bar.
* If the platform is running Jelly Bean or earlier, or translucent system
* UI modes have not been enabled in either the theme or via window flags,
* then this method does nothing.
* @param enabled True to enable tinting, false to disable it (default).
public void setStatusBarTintEnabled(boolean enabled) {
mStatusBarTintEnabled =
if (mStatusBarAvailable) {
mStatusBarTintView.setVisibility(enabled ? View.VISIBLE : View.GONE);
* Enable tinting of the system navigation bar.
* If the platform does not have soft navigation keys, is running Jelly Bean
* or earlier, or translucent system UI modes have not been enabled in either
* the theme or via window flags, then this method does nothing.
* @param enabled True to enable tinting, false to disable it (default).
public void setNavigationBarTintEnabled(boolean enabled) {
mNavBarTintEnabled =
if (mNavBarAvailable) {
mNavBarTintView.setVisibility(enabled ? View.VISIBLE : View.GONE);
* Apply the specified color tint to all system UI bars.
* @param color The color of the background tint.
public void setTintColor(int color) {
setStatusBarTintColor(color);
setNavigationBarTintColor(color);
* Apply the specified drawable or color resource to all system UI bars.
* @param res The identifier of the resource.
public void setTintResource(int res) {
setStatusBarTintResource(res);
setNavigationBarTintResource(res);
* Apply the specified drawable to all system UI bars.
* @param drawable The drawable to use as the background, or null to remove it.
public void setTintDrawable(Drawable drawable) {
setStatusBarTintDrawable(drawable);
setNavigationBarTintDrawable(drawable);
* Apply the specified alpha to all system UI bars.
* @param alpha The alpha to use
public void setTintAlpha(float alpha) {
setStatusBarAlpha(alpha);
setNavigationBarAlpha(alpha);
* Apply the specified color tint to the system status bar.
* @param color The color of the background tint.
public void setStatusBarTintColor(int color) {
if (mStatusBarAvailable) {
mStatusBarTintView.setBackgroundColor(color);
* Apply the specified drawable or color resource to the system status bar.
* @param res The identifier of the resource.
public void setStatusBarTintResource(int res) {
if (mStatusBarAvailable) {
mStatusBarTintView.setBackgroundResource(res);
* Apply the specified drawable to the system status bar.
* @param drawable The drawable to use as the background, or null to remove it.
@SuppressWarnings("deprecation")
public void setStatusBarTintDrawable(Drawable drawable) {
if (mStatusBarAvailable) {
mStatusBarTintView.setBackgroundDrawable(drawable);
* Apply the specified alpha to the system status bar.
* @param alpha The alpha to use
@TargetApi(11)
public void setStatusBarAlpha(float alpha) {
if (mStatusBarAvailable && Build.VERSION.SDK_INT &= Build.VERSION_CODES.HONEYCOMB) {
mStatusBarTintView.setAlpha(alpha);
* Apply the specified color tint to the system navigation bar.
* @param color The color of the background tint.
public void setNavigationBarTintColor(int color) {
if (mNavBarAvailable) {
mNavBarTintView.setBackgroundColor(color);
* Apply the specified drawable or color resource to the system navigation bar.
* @param res The identifier of the resource.
public void setNavigationBarTintResource(int res) {
if (mNavBarAvailable) {
mNavBarTintView.setBackgroundResource(res);
* Apply the specified drawable to the system navigation bar.
* @param drawable The drawable to use as the background, or null to remove it.
@SuppressWarnings("deprecation")
public void setNavigationBarTintDrawable(Drawable drawable) {
if (mNavBarAvailable) {
mNavBarTintView.setBackgroundDrawable(drawable);
* Apply the specified alpha to the system navigation bar.
* @param alpha The alpha to use
@TargetApi(11)
public void setNavigationBarAlpha(float alpha) {
if (mNavBarAvailable && Build.VERSION.SDK_INT &= Build.VERSION_CODES.HONEYCOMB) {
mNavBarTintView.setAlpha(alpha);
* Get the system bar configuration.
* @return The system bar configuration for the current device configuration.
public SystemBarConfig getConfig() {
* Is tinting enabled for the system status bar?
* @return True if enabled, False otherwise.
public boolean isStatusBarTintEnabled() {
return mStatusBarTintE
* Is tinting enabled for the system navigation bar?
* @return True if enabled, False otherwise.
public boolean isNavBarTintEnabled() {
return mNavBarTintE
private void setupStatusBarView(Context context, ViewGroup decorViewGroup) {
mStatusBarTintView = new View(context);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, mConfig.getStatusBarHeight());
params.gravity = Gravity.TOP;
if (mNavBarAvailable && !mConfig.isNavigationAtBottom()) {
params.rightMargin = mConfig.getNavigationBarWidth();
mStatusBarTintView.setLayoutParams(params);
mStatusBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR);
mStatusBarTintView.setVisibility(View.GONE);
decorViewGroup.addView(mStatusBarTintView);
private void setupNavBarView(Context context, ViewGroup decorViewGroup) {
mNavBarTintView = new View(context);
if (mConfig.isNavigationAtBottom()) {
params = new LayoutParams(LayoutParams.MATCH_PARENT, mConfig.getNavigationBarHeight());
params.gravity = Gravity.BOTTOM;
params = new LayoutParams(mConfig.getNavigationBarWidth(), LayoutParams.MATCH_PARENT);
params.gravity = Gravity.RIGHT;
mNavBarTintView.setLayoutParams(params);
mNavBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR);
mNavBarTintView.setVisibility(View.GONE);
decorViewGroup.addView(mNavBarTintView);
* Class which describes system bar sizing and other characteristics for the current
* device configuration.
public static class SystemBarConfig {
private static final String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height";
private static final String NAV_BAR_HEIGHT_RES_NAME = "navigation_bar_height";
private static final String NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME = "navigation_bar_height_landscape";
private static final String NAV_BAR_WIDTH_RES_NAME = "navigation_bar_width";
private static final String SHOW_NAV_BAR_RES_NAME = "config_showNavigationBar";
private final boolean mTranslucentStatusB
private final boolean mTranslucentNavB
private final int mStatusBarH
private final int mActionBarH
private final boolean mHasNavigationB
private final int mNavigationBarH
private final int mNavigationBarW
private final boolean mInP
private final float mSmallestWidthDp;
private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {
Resources res = activity.getResources();
mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
mSmallestWidthDp = getSmallestWidthDp(activity);
mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
mActionBarHeight = getActionBarHeight(activity);
mNavigationBarHeight = getNavigationBarHeight(activity);
mNavigationBarWidth = getNavigationBarWidth(activity);
mHasNavigationBar = (mNavigationBarHeight & 0);
mTranslucentStatusBar = translucentStatusB
mTranslucentNavBar = traslucentNavB
@TargetApi(14)
private int getActionBarHeight(Context context) {
int result = 0;
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
result = plexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
@TargetApi(14)
private int getNavigationBarHeight(Context context) {
Resources res = context.getResources();
int result = 0;
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
if (hasNavBar(context)) {
if (mInPortrait) {
key = NAV_BAR_HEIGHT_RES_NAME;
key = NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME;
return getInternalDimensionSize(res, key);
@TargetApi(14)
private int getNavigationBarWidth(Context context) {
Resources res = context.getResources();
int result = 0;
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
if (hasNavBar(context)) {
return getInternalDimensionSize(res, NAV_BAR_WIDTH_RES_NAME);
@TargetApi(14)
private boolean hasNavBar(Context context) {
Resources res = context.getResources();
int resourceId = res.getIdentifier(SHOW_NAV_BAR_RES_NAME, "bool", "android");
if (resourceId != 0) {
boolean hasNav = res.getBoolean(resourceId);
// check override flag (see static block)
if ("1".equals(sNavBarOverride)) {
} else if ("0".equals(sNavBarOverride)) {
return hasN
} else { // fallback
return !ViewConfiguration.get(context).hasPermanentMenuKey();
private int getInternalDimensionSize(Resources res, String key) {
int result = 0;
int resourceId = res.getIdentifier(key, "dimen", "android");
if (resourceId & 0) {
result = res.getDimensionPixelSize(resourceId);
@SuppressLint("NewApi")
private float getSmallestWidthDp(Activity activity) {
DisplayMetrics metrics = new DisplayMetrics();
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.JELLY_BEAN) {
activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
// TODO this is not correct, but we don‘t really care pre-kitkat
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
float widthDp = metrics.widthPixels / metrics.
float heightDp = metrics.heightPixels / metrics.
return Math.min(widthDp, heightDp);
* Should a navigation bar appear at the bottom of the screen in the current
* device configuration? A navigation bar may appear on the right side of
* the screen in certain configurations.
* @return True if navigation should appear at the bottom of the screen, False otherwise.
public boolean isNavigationAtBottom() {
return (mSmallestWidthDp &= 600 || mInPortrait);
* Get the height of the system status bar.
* @return The height of the status bar (in pixels).
public int getStatusBarHeight() {
return mStatusBarH
* Get the height of the action bar.
* @return The height of the action bar (in pixels).
public int getActionBarHeight() {
return mActionBarH
* Does this device have a system navigation bar?
* @return True if this device uses soft key navigation, False otherwise.
public boolean hasNavigtionBar() {
return mHasNavigationB
* Get the height of the system navigation bar.
* @return The height of the navigation bar (in pixels). If the device does not have
* soft navigation keys, this will always return 0.
public int getNavigationBarHeight() {
return mNavigationBarH
* Get the width of the system navigation bar when it is placed vertically on the screen.
* @return The width of the navigation bar (in pixels). If the device does not have
* soft navigation keys, this will always return 0.
public int getNavigationBarWidth() {
return mNavigationBarW
* Get the layout inset for any system UI that appears at the top of the screen.
* @param withActionBar True to include the height of the action bar, False otherwise.
* @return The layout inset (in pixels).
public int getPixelInsetTop(boolean withActionBar) {
return (mTranslucentStatusBar ? mStatusBarHeight : 0) + (withActionBar ? mActionBarHeight : 0);
* Get the layout inset for any system UI that appears at the bottom of the screen.
* @return The layout inset (in pixels).
public int getPixelInsetBottom() {
if (mTranslucentNavBar && isNavigationAtBottom()) {
return mNavigationBarH
* Get the layout inset for any system UI that appears at the right of the screen.
* @return The layout inset (in pixels).
public int getPixelInsetRight() {
if (mTranslucentNavBar && !isNavigationAtBottom()) {
return mNavigationBarW
public void setStatusBarDarkMode(boolean darkmode, Activity activity) {
if (sIsMiuiV6) {
Class&? extends Window& clazz = activity.getWindow().getClass();
int darkModeFlag = 0;
Class&?& layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
} catch (Exception e) {
e.printStackTrace();
public void initSystemBar(){
if (VERSION.SDK_INT &= VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
fwRootLayout.setFitsSystemWindows(true);//需要把根布局设置为这个属性 子布局则不会占用状态栏位置
fwRootLayout.setClipToPadding(true);//需要把根布局设置为这个属性 子布局则不会占用状态栏位置
tintManager = new SystemBarTintManager(this);// 创建状态栏的管理实例
tintManager.setStatusBarTintEnabled(true);// 激活状态栏设置
tintManager.setNavigationBarTintEnabled(true);// 激活导航栏设置
tintManager.setStatusBarTintColor(getResources().getColor(R.color.blue500));//设置状态栏颜色
tintManager.setStatusBarDarkMode(false, this);//false 状态栏字体颜色是白色 true 颜色是黑色
以上所述是小编给大家介绍的Android 沉浸式改变小米魅族状态栏颜色的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对脚本之家网站的支持!Android系统更改状态栏字体颜色_Android每日干货_传送门
Android系统更改状态栏字体颜色
Android每日干货
随着时代的发展,Android的状态栏都不是乌黑一片了,在Android4.4之后我们可以修改状态栏的颜色或者让我们自己的View延伸到状态栏下面。我们可以进行更多的定制化了,然而有的时候我们使用的是淡色的颜色比如白色,由于状态栏上面的文字为白色,这样的话状态栏上面的文字就无法看清了。因此本文提供一些解决方案,可以是MIUI6+,Flyme4+,Android6.0+支持切换状态栏的文字颜色为暗色。
public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
Class clazz = activity.getWindow().getClass();
int darkModeFlag = 0;
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
} catch (Exception e) {
e.printStackTrace();
上面为小米官方提供的解决方案,主要为MIUI内置了可以修改状态栏的模式,支持Dark和Light两种模式。修改Flymepublic static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
Class clazz = activity.getWindow().getClass();
int darkModeFlag = 0;
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
} catch (Exception e) {
e.printStackTrace();
同理使用跟miui类似的方式修改Android6.0+
Android 6.0开始,谷歌官方提供了支持,在style属性中配置 android:windowLightStatusBar即可, 设置为 true 时,当statusbar的背景颜色为淡色时,statusbar的文字颜色会变成灰色,为 false 时同理。
目前为止,android6.0的市场占有率还很少,而MIUI和flyme在国内占有率还算可以,因此,我们可以尽自己所能,适配更多。如果你还有其他的奇淫技巧,也欢迎分享补充。
觉得不错,分享给更多人看到
Android每日干货 微信二维码
分享这篇文章
7月1日 23:16
Android每日干货 最新头条文章
Android每日干货 热门头条文章}

我要回帖

更多关于 安卓改变状态栏颜色 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信