android 隐藏导航栏如何使系统标题栏颜色和导航栏颜色

经验2076 米
在线时间19 小时
机型小米手机4
签到次数158
MIUI版本8.5.8
我做主题的时候,把默认的灰色标题栏改为淡红色的,但是此时状态栏文字颜色却是黑色的,如下图:
Screenshot_-10-44-43_com.android.settin.png (125.43 KB, 下载次数: 2)
我想让它变成白色的,这样才好看。因为我看他默认的应用,比如安全中心,日历什么的,进去后,状态栏的颜色都变成白色的,我想请教的就是这个该怎么设置???
对应的例子就像网易云音乐这样变成白色:
Screenshot_-15-12-41_com.netease.cloudm.png (921.14 KB, 下载次数: 2)
分享到微信朋友圈
打开微信,点击底部的“发现”,使用 “扫一扫” 即可将网页分享到我的朋友圈。
经验2854 米
在线时间261 小时
版本8.4.26
MIUI9优先体验组
积分 3628, 距离下一级还需 1372 积分
积分 3628, 距离下一级还需 1372 积分
机型未知设备
签到次数82
MIUI版本8.4.26
网易的那是因为APP自带代码,自动向MIUI请求变色。
经验556 米
在线时间22 小时
版本V8.5.7.0.MCFCNED
积分 606, 距离下一级还需 1394 积分
积分 606, 距离下一级还需 1394 积分
机型未知设备
签到次数22
MIUI版本V8.5.7.0.MCFCNED
同问,有没有高手来解决一下?小伙伴解决了告诉我一下
经验2076 米
在线时间19 小时
机型小米手机4
签到次数158
MIUI版本8.5.8
网易的那是因为APP自带代码,自动向MIUI请求变色。
这个我知道,我的意思是怎么样才能像那样变为白色,需要修改哪里
经验2854 米
在线时间261 小时
版本8.4.26
MIUI9优先体验组
积分 3628, 距离下一级还需 1372 积分
积分 3628, 距离下一级还需 1372 积分
机型未知设备
签到次数82
MIUI版本8.4.26
这个我知道,我的意思是怎么样才能像那样变为白色,需要修改哪里
应该是需要添加该应用的一个单独模块,具体代码不知。
经验140 米
在线时间15 小时
版本5.6.11
积分 166, 距离下一级还需 34 积分
积分 166, 距离下一级还需 34 积分
机型未知设备
MIUI版本5.6.11
问题是进入联系人等其它界面还是默认变成黑色的,就算你的背景 是深色也还是黑色字体!同求怎么更改颜色,或改掉自动适配颜色的设置!!!!
06.png (734.16 KB, 下载次数: 0)
经验2866 米
在线时间18 小时
版本V9.5.4.0.NADCNFA
积分 3091, 距离下一级还需 1909 积分
积分 3091, 距离下一级还需 1909 积分
机型未知设备
签到次数79
MIUI版本V9.5.4.0.NADCNFA
解锁申请N天后终于通过了审核,结果解锁时提示&解锁账号与手机中登陆的账号不一致&。系统已经是开发版了。。。重启,重新登陆账号,重新云同步,重置手机,都试过了,P用都灭有。。我也是醉了。。。谁知道这是怎么回事啊
经验5273 米
在线时间60 小时
机型未知设备
签到次数74
MIUI版本8.6.7
楼主,你信号左边哪个像锁一样的东西是怎么弄的?
经验5818 米
在线时间110 小时
版本7.11.16
积分 6148, 距离下一级还需 13852 积分
积分 6148, 距离下一级还需 13852 积分
机型未知设备
签到次数72
MIUI版本7.11.16
感谢楼主分享
经验1558 米
在线时间4 小时
版本V9.6.1.0.OAACNFD
积分 1561, 距离下一级还需 439 积分
积分 1561, 距离下一级还需 439 积分
机型未知设备
签到次数173
MIUI版本V9.6.1.0.OAACNFD
可以试试这个
MIUI 10专属勋章
参加回帖活动
MIUI七周年
MIUI 9纪念勋章
1000万用户纪念勋章
MIUI1000万用户纪念勋章
新版论坛APP
更新新版APP
已关注微信
关注腾讯微博
已关注腾讯微博
关注新浪微博
已关注新浪微博
MIUI 7纪念勋章
2018新年勋章
参与回帖活动
万圣节勋章
参加回帖活动
2017圣诞节勋章
参与回帖活动
感恩节勋章
参与回帖活动
米兔月饼勋章
参加回帖活动
MIUI七夕鹊桥勋章
小米7周年勋章
2017米粉节晒单赢专属勋章
APP 1000万
MIUI论坛APP注册用户突破1000万纪念勋章
MIUI五周年
MIUI五周年纪念勋章
已关注极客秀微信
Copyright (C) 2017 MIUI
京ICP备号 | 京公网安备34号 | 京ICP证110507号android状态栏颜色修改 - leon_hm - 博客园
android状态栏颜色修改
状态栏颜色的修改在4.4和5.x环境下分别有不同的方式,低于4.4以下是不能修改的。
方式一,状态栏将显示为纯净的颜色,没有渐变效果
* 状态栏相关工具类
public class StatusBarUtils {
public static void setWindowStatusBarColor(Activity activity, int colorResId) {
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(activity.getResources().getColor(colorResId));
//底部导航栏
//window.setNavigationBarColor(activity.getResources().getColor(colorResId));
} catch (Exception e) {
e.printStackTrace();
public static void setWindowStatusBarColor(Dialog dialog, int colorResId) {
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.LOLLIPOP) {
Window window = dialog.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(dialog.getContext().getResources().getColor(colorResId));
//底部导航栏
//window.setNavigationBarColor(activity.getResources().getColor(colorResId));
} catch (Exception e) {
e.printStackTrace();
效果图如下:状态栏被改成android.R.color.holo_blue_bright,标题栏颜色可以和状态栏一样,EditText的输入也没有受影响
ps:如果顶部为渐变效果,可能是在主题中设置windowTranslucentStatus=true属性。
通过Style来修改状态栏颜色。
1.设置 colorPrimary,colorPrimaryDark两个颜色。
&stylename="AppTheme"parent="Theme.AppCompat.Light.NoActionBar"&
&itemname="colorPrimary"&@android:color/holo_blue_bright&/item&
&itemname="colorPrimaryDark"&@android:color/holo_blue_bright&/item&
2. AndroidManifest.xml文件中的targetSdkVersion必须设置在21以上。
3.parent主题必须是Theme.AppCompat开头,兼容包下的主题,所以必须一用v7包。
colorPrimary,colorPrimaryDark这两个属性是Material Design风格中规定的。具体位置如下图所示:
1.在res/values-v19文件夹下添加styles.xml文件内容如下
&stylename="AppTheme"parent="@style/BaseAppTheme"&
&itemname="android:windowTranslucentStatus"&true&/item&
2.顶部标题的控件设置两个属
android:background="@android:color/holo_blue_bright"
android:fitsSystemWindows="true"
则状态栏会保持与设置fitsSystemWindow属性的控件的背景颜色一致。
上面的方式三也适用4.4环境。不过4.4和5.x下显示的效果有差异。根据本人测试结果来看,不同的手机厂商对于这种情况下,状态栏有的是渐变,有的是添加了一层黑色半透明层。
存在bug及解决办法
修改windowTranslucentStatus/Navigation="true"。 会导致EditText输入时,即使使用了 adjustResize,软键盘也会挡住 EditText
解决办法参考:
其他参考资料:1. 隐藏当前Activity标题栏
& & 在当前Activity中调用:this.requestWindowFeature(Window.FEATURE_NO_TITLE);
2. 隐藏当前Activity状态栏(Status Bar)
2.1&Android 4.0 and Lower
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If the Android version is lower than Jellybean, use this call to hide
// the status bar.
if (Build.VERSION.SDK_INT & 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
2.2&Android 4.1 and Higher
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();
3. 隐藏当前Activity界面的导航栏(NavigationBar)
& & 在Android4.0及以后版本中,可通过以下方法隐藏NavigationBar
View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
4. 隐藏所有Activity界面的标题栏
 修改AndroidManifest.xml&  在application 标签中添加a & & android:theme=" :style/Theme.NoTitleBar"
5. 隐藏所有Activity界面的TitleBar 和StatusBar&
  修改AndroidManifest.xml&   在application 标签中添加&
  android:theme=" :style/Theme.NoTitleBar.Fullscreen"
本文转载自:http://blog.csdn.net/myarrow/article/details/
人打赏支持
码字总数 1876
转载时请记得标明源地址:http://my.oschina.net/lijindou/blog 1.将系统的状态栏沉浸 getWindow().addFlags(WindowManager.LayoutParams.FLAGTRANSLUCENTSTATUS); //透明导航栏 getWindow(......
紫火晗风 ?
获取View类界面控件的位置,有助于添加新的控件。 获取在parent里的相对坐标位置 这个比较简单,不用多说,直接调用View的方法:getLeft , getTop, getBottom, getRight 获得。 获取在屏幕中...
在一个应用程序中,一般都会存在多个Activity,每个Activity对应着一个UI布局文件。一般来说,为了保持不同窗口之间的风格统一,在这些UI布局文件中,几乎肯定会用到很多相同的布局。如果我们...
KingMing ?
小米Launcher有一个细节上的功能效果:在长按桌面应用图标时,会隐藏状态栏,然后在状态栏原有的布局上显示卸载或删除的操作栏。放手后,操作栏隐藏,状态栏显示出来。也就是说,这个过程是涉...
不正经啊不正经 ?
隐藏标题栏需要使用预定义样式:android:theme=”@android :style/Theme.NoTitleBar”. 隐藏状态栏:android:theme=”@android :style/Theme.NoTitleBar.Fullscreen”. [代码] [XML]代码 [代...
因为Android官方从来没有给出过沉浸式状态栏这样的命名,只有沉浸式模式(Immersive Mode)这种说法。而有些人在没有完全了解清楚沉浸模式到底是什么东西的情况下,就张冠李戴地认为一些系统...
津乐 ? 04/20 ?
淡化状态栏和系统栏 如果要淡化状态和通知栏,在版本为4.0以上的Android系统上,你可以像如下使用这个标签。 一旦用户触摸到了状态栏或者是系统栏,这个标签就会被清除,使系统栏重新显现(无...
操作栏是应用(App)的activity最重要的设计元素之一。它提供一些涉及用户界面的功能,通过提供跨不同应用的统一行为,将可以使你的应用让用户立即感觉熟悉。一些关键的功能包括: 为你应用的...
夜游长空 ?
Activity设置全屏显示(隐藏标题栏和状态栏): 方法一: 在Activity oncreate()方法中 //隐去电池等图标和一切修饰部分(状态栏部分) this.getWindow().setFlags(WindowManager.LayoutParams...
SRain215 ?
要设置弹出键盘是否覆盖Activity的view,或者软键盘的显示隐藏状态,需要用到Activity的一个属性: android:windowSoftInputMode 该属性在AndroidManifest.xml中设置,如下: &activity andr...
Remix_jx ?
没有更多内容
加载失败,请刷新页面
今天在用yum安装软件包的时候出现如下错误信息(这只是部分): Installing : pango-1.28.1-11.el6.x86_64 1/9 Error unpacking rpm package pango-1.28.1-11.el6.x86_64 error: unpacking ......
晨风993 ? 13分钟前 ?
架构漫谈是由资深架构师王概凯Kevin执笔的系列专栏,专栏将会以Kevin的架构经验为基础,逐步讨论什么是架构、怎样做好架构、软件架构如何落地、如何写好程序等问题。专栏的目的是希望能抛出一...
SuShine ? 22分钟前 ?
针对某些绿色软件包,当我们移动软件包的位置时,再次设置默认打开方式会出现无法设置的情况。 ①选择要设置的文件-&右击-&打开方式-&选择默认程序 ②浏览选择默认打开方式的应用 ③点击打开...
clin003 ? 49分钟前 ?
Specifmax key length is 767 bytes https://blog.csdn.net/gladyoucame/article/details/ https://blog.csdn.net/shaochenshuo/article/details/ htt......
安小乐 ? 今天 ?
《Tensorflow基础快速入门》的目的是帮助广大的深度学习爱好者,逐层深入,步步精通当下最流行的深度学习框架Tensorflow。该课程包含Tensorflow运行原理,Tensor上面常见的操作,常见API的使...
liwei2000 ? 今天 ?
1、引言 MIT TR 35(MIT Technology Review 35 Innovators Under 35)——“全球 35 位 35 岁以下科技创新青年”榜单,是全球最权威的青年科技创新人才榜单之一。从1999年开始,《麻省理工科...
JackJiang- ? 今天 ?
public class IPUtil {
public static String getIpAddress(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.......
晓星沉 ? 今天 ?
遇到了一个很蠢的问题: The origin server did not find a current representation for the target resource or is not willing to disclose that one exist 然后逐一排查原来是路径问题!资......
举个_栗子 ? 今天 ?
SELECT * FROM ( SELECT re_unid, COUNT(re_unid) 是否重复 FROM `re_intv_written_english` WHERE st_unid = 'd2-4ead-ab1f-017bc9b17a11' GROUP BY re_unid)AS b WHERE 是否重复......
lanjian28 ? 今天 ?
从mysql增量导出到hive库 增量导入参数:--check-column Source column to check for incremental --incremental Define an incremental import of type --last-value Last imported value i......
hblt-j ? 今天 ?
没有更多内容
加载失败,请刷新页面
文章删除后无法恢复,确定取消删除此文章吗?
亲,自荐的博客将通过私信方式通知管理员,优秀的博客文章审核通过后将在博客推荐列表中显示
确定推荐此文章吗?
确定推荐此博主吗?
聚合全网技术文章,根据你的阅读喜好进行个性推荐
指定官方社区
深圳市奥思网络科技有限公司版权所有博客分类:
Android到目前为止好象还没有公开可以修改标题栏背景色的API,网上提供的解决方案一般是做一张对应颜色的文件,再通过style.xml更改标题栏样式,类似解决方法如:http://blog.sina.com.cn/s/blog_0rblo.html,这是一种静态配置的方式。
我的项目中为了支持用户能够以所见即所得的方式来选择标题栏颜色,因此采用了一种通过程序的方法动态设置,代码如下:
View bv = this.findViewById(android.R.id.title
((TextView) bv).setTextColor(Color.WHITE);
((View) bv.getParent()).setBackgroundColor(Color.RED);
这样,即可设置标题栏背景为红色,字体为白色
当然,可以看出,这并不是使用Google公开的API,而是一个HackMethod,在未来的版本中可能会失效,但可能性不大
浏览 26193
wander754085
浏览: 42383 次
来自: 北京
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'Android自定义状态栏颜色与应用标题栏颜色一致
转载 &更新时间:日 14:31:24 & 作者:lplj717
看IOS上的应用,应用中状态栏的颜色总能与应用标题栏颜色保持一致,用户体验很不错,对于这种效果怎么实现的呢?下面小编给大家分享android自定义状态栏颜色与应用标题栏颜色一致的实现方法,一起看看吧
每次看IOS上的应用,应用中状态栏的颜色总能与应用标题栏颜色保持一致,用户体验很不错,对于这种效果,像我这种好奇心强的人就会去看看那安卓是否可以呢?若是在安卓4.4之前,答案是否定的,但在4.4之后,谷歌允许开发者自定义状态栏背景颜色啦,这是个不错的体验!若你手机上安装有最新版的qq,并且你的安卓SDK版本是4.4及以上,你可以看下它的效果:
实现此功能有两种方法:
1.在xml中设置主题或自定义style;
Theme.Holo.Light.NoActionBar.TranslucentDecor
Theme.Holo.NoActionBar.TranslucentDecor
&style name="AppTheme" parent="AppBaseTheme"&
&!-- Status Bar --&
&item name="android:windowTranslucentStatus"&true&/item&
&!-- Navigation Bar --&
&item name="android:windowTranslucentNavigation"&true&/item&
鉴于市面上各种手机的SDK的各种版本,不建议采用这种方法;
2.在代码中控制;
首先创建一个Activity,在onCreate方法中进行处理:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.barcolor);//通知栏所需颜色
setContentView(R.layout.activity_main);
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
winParams.flags |=
winParams.flags &= ~
win.setAttributes(winParams);
需注意的是, tintManager.setStatusBarTintResource(R.color.top_bg_color);这一步的颜色值(即把你的状态栏颜色与你的标题栏颜色保持一致)要写在color.xml中去,如果用Color.praseColor则会报错。
SystemBarTintManager.java文件
package com.test.
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.M
* Created by lplj on .
* Class to manage status and navigation bar tint effects when using KitKat
* translucent system UI modes.
@SuppressWarnings({ "rawtypes", "unchecked" })
public class SystemBarTintManager {
// Android allows a system property to override the presence of the navigation bar.
// Used by the emulator.
// See https://github.com/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);
sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
} catch (Throwable e) {
sNavBarOverride =
* 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
* 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 = TypedValue.complexToDimensionPixelSize(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
接下来我们看看效果图(前一个图是没有设置颜色的效果图,后一个是设置颜色的效果图):
貌似已经达到效果了,但仔细观察,好像标题栏被提上去了,就是说APP界面全屏了,状态了盖在了APP上,恩,这并非我们想要的效果,那如何将界面从状态栏下部开始呢,只需要在Activity的布局文件最外层控件加上一个属性:
android:fitsSystemWindows="true"就可以啦!看下效果:
大功告成!
PS:在使用过程中发现了一些问题,使用以上方法对单个Activity有效,但是对继承了TabActivity的导航页怎么办呢?假如MainActivity继承了TabActivity,Tab1Activity、Tab2Activity、Tab3Activity是三个子项,那么设置状态栏的代码需写在MainActivity中,而 android:fitsSystemWindows="true"需写在三个子Activity的xml布局文件中,这样设置后仍然有问题,就是进入应用后首页也就是Tab1Activity没有问题,而Tab2Activity、Tab3Activity却没达到效果,它们的效果相当于未加android:fitsSystemWindows="true"时的效果,期初我怀疑是Activity不同的原因,因此我把Tab1Activity和Tab3Activity调了下位置,结果Tab3Activity成为首页后正常,而Tab1Activity又不正常了,百思不得姐,最后实在没办法,就在Tab2Activity、Tab3Activity的OnCreate方法中加了几句代码:
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.KITKAT) {
((LinearLayout)findViewById(R.id.ll)).setPadding(0,
SysUtils.getStatusHeight(this), 0,0);
意思是,先求出状态栏高度,然后设置最外层控件的PaddingTop值为状态栏高度,结果正好达到效果,至于为什么只有首页Activity可以达到效果,而后面的子项无法达到效果,本人也在郁闷中,有知道的朋友可以分享下!
状态栏高度算法:
* 状态栏高度算法
* @param activity
public static int getStatusHeight(Activity activity){
int statusHeight = 0;
Rect localRect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
statusHeight = localRect.
if (0 == statusHeight){
Class&?& localC
localClass = Class.forName("com.android.internal.R$dimen");
Object localObject = localClass.newInstance();
int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
statusHeight = activity.getResources().getDimensionPixelSize(i5);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
return statusH
伴随着Android5.0的发布也更新了support-v7-appcompat 到V21,其中增加了ToolBar,recyclerview,cardview等控件。
Android5.0对改变APP样式下了不小的功夫,写法也和以前不一样了,这里也简单介绍一下
colorAccent,colorPrimary,colorPrimaryDark的作用:
这样是不是清晰很多了,继续加油吧,编程的路还很长,我们慢慢走
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具

我要回帖

更多关于 android 导航栏透明 的文章

 

随机推荐