如何实现Android安卓沉浸式状态栏实现

Android标准、固定和沉浸式状态栏的实现
总结了一下Android状态栏的颜色设置,常见的有固定的颜色或者沉浸式的实现,废话不多说,直接上图。 标准: 固定颜色: 沉浸式: 我个人最新沉浸式的实现,但是这个功能只支持A
总结了一下Android状态栏的颜色设置,常见的有固定的颜色或者沉浸式的实现,废话不多说,直接上图。
固定颜色:
我个人最新沉浸式的实现,但是这个功能只支持Android4.4以上的系统。
下面来说一下具体的实现过程。
首先,标准的就不说了,默认的就是。然后再说沉浸,最复杂的放后面。
沉浸很简单,两句代码搞定:
在Activity中的setContentView(R.layout.activity_main);之前加上:
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.KITKAT) {
// 透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// 透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
再说固定颜色的设置,需要一个依赖的java文件,先贴出来等会要用。还有一个要注意的地方,这种方式需要在当前activity的布局文件中的最外层加上android:fitsSystemWindows=&true&这个属性。
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
@SuppressWarnings({ &unchecked&, &rawtypes& })
public class SystemStatusManager
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 SystemStatusManager(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);
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
好了,然后我把主activity贴出来吧。
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTranslucentStatus();
setContentView(R.layout.activity_main);
private void setTranslucentStatus() {
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.KITKAT) {
// 透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// 透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
SystemStatusManager tintManager = new SystemStatusManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.red);//你要设置的颜色
getWindow().getDecorView().setFitsSystemWindows(true);
好了,这就实现了上面效果图的第二张,固定的状态栏颜色。
(责任编辑:最模板)
------分隔线----------------------------
Android使用的是基于Linux的文件系统,对于文件的访问和管理是通过...
我们在用Android中的Canvas绘制各种图形时,可以通过Paint.setShade...
实际开发经常有这种情况,比如登录请求,返回来的并不会仅仅...
android 自定义控件属性,学到了TypedArray以及attrs。讲一下流程吧...
除了程序计数器外,虚拟机内存的其他几个运行时区域都有发生...
CopyRight (C)
最模板 , All Rights Reserved.MIUI 6 沉浸式状态栏调用方法 | 小米应用开发者文档
看着iOS美腻腻的沉浸,看着MIUI 6 美腻腻的系统App沉浸,看着自己黑不溜秋的状态栏!强迫症的你还能忍受吗?今天,正式启动强迫症患者拯救行动!快来将你的应用也实现完美沉浸吧!
Demo下载:
一、什么是沉浸式状态栏
默认情况的状态栏是不透明白色字体的状态栏样式,如下图:
目前MIUI 6的完美沉浸已经支持透明白色字体和透明黑色字体
二、沉浸式状态栏实现代码
第三方应用需要用反射调用,参考代码:
miuiv6只支持4.4及以上版本,调用状态栏透明的方法可以直接用原生的安卓方法
@TargetApi(19)
protected 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);
下面是调用状态栏 是否为darkmode。
public void setStatusBarDarkMode(boolean darkmode, Activity activity) {
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();
沉浸式菜单栏只能在MIUI 6的系统上实现,其他安卓系统没有效果。
沉浸式效果对非MIUI系统的兼容性不会有任何影响。
google的actionbar存在bug,不支持沉浸代码。
Demo下载:
This entry was posted in查看: 1333|回复: 4
沉浸式状态栏如何改变 状态栏图标颜色与文字颜色
签到天数: 24 天连续签到: 1 天[LV.4]偶尔看看III主题帖子e币
如果我把状态栏改成了白色,状态栏图标和文字完全看不见,求助~~~~~~~~~··
附件: 您需要
才可以下载或查看,没有帐号?
签到天数: 84 天连续签到: 2 天[LV.6]常住居民II主题帖子e币
签到天数: 169 天连续签到: 1 天[LV.7]常住居民III主题帖子e币
http://blog.csdn.net/tanranran/article/details/
签到天数: 169 天连续签到: 1 天[LV.7]常住居民III主题帖子e币
你这是给他盖住了吧
该用户从未签到主题帖子e币
大家都是白色。。当然看不到了,,谷歌官方是将状态栏设置成半透明,,谷歌没有绘制灰色的状态栏图标,,这个没办法了,国内魅族小米的api就有灰色的,不过要独立适配
推荐阅读热门话题
619831816415412369318266257253249249232223215715
半小时前半小时前半小时前1&小时前1&小时前1&小时前2&小时前2&小时前2&小时前2&小时前2&小时前2&小时前3&小时前3&小时前4&小时前4&小时前
Powered by今天看啥 热点:
android沉浸式状态栏实现
传统的手机状态栏是呈现出黑色条状的,有的和手机主界面有很明显的区别。这样就在一定程度上牺牲了视觉宽度,界面面积变小。
沉浸模式的状态栏和主界面完全融为了一体,在设计上有不同的视觉感受。
我们先上两张图,很容易看出区别:
Android在4.4的时候增加了透明状态栏与导航栏的功能,依托于这个新特性,我们可以开始跟随潮流,实现Android的沉浸式状态栏 其实上图展示的这个关于界面的代码非常简单
* 关于界面
* @author SuS
public class AboutActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_about);
setImmerseLayout(findViewById(mon_back));
initBackButton();
setTitleBar(R.string.durian_about);
protected void onResume() {
super.onResume();
protected void onPause() {
super.onPause();
protected void onDestroy() {
super.onDestroy();
现在请注意setImmerseLayout()这个方法,这个方法是在BaseActivity中实现的
public class BaseActivity extends FragmentActivity {
private static final String TAG = BaseA
...............
public void initBackButton() {
ImageView backButton = (ImageView) this.findViewById(R.id.durian_back_image);
backButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finishActivity();
protected void setImmerseLayout(View view) {
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
/*window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);*/
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
int statusBarHeight = ScreenUtil.getStatusBarHeight(this.getBaseContext());
view.setPadding(0, statusBarHeight, 0, 0);
public void finishActivity() {
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
public void setTitleBar(int id) {
TextView tvName = (TextView) findViewById(R.id.durian_title_text);
tvName.setText(id);
使用 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 或者 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
都可以使状态栏透明,但是关键是下面这两行代码,如果没有这两行,会是这样
那么这两行神奇的代码的原理是什么? 我们先看一下ScreenUtil中的getStatusBarHeight方法
* 用于获取状态栏的高度。 使用Resource对象获取(推荐这种方式)
* @return 返回状态栏高度的像素值。
public static int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier(status_bar_height, dimen,
if (resourceId & 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
这里是获得状态栏的高度,然后我们就可以通过设置common_back的padding属性 即:view.setPadding(0, statusBarHeight, 0, 0)来达到终极效果 但是这里还是需要注意细节的,首先大家应该理解padding的含义:内边距 那么再看一下common_back的布局文件 在activity_about.xml中我们是使用include引用的common_back
common_back的布局如下:
&framelayout android:background="@color/common_top_bg" android:id="@+id/durian_head_layout" android:layout_height="wrap_content" android:layout_width="match_parent" xmlns:android="/apk/res/android" xmlns:tools="/tools"&
&/framelayout&
这里我们使用了一层FrameLayout包裹住RelativeLayout,这里有什么玄机那,其实这里就是为了 方便上面那两行神奇的代码起作用,这样我们就能设置RelativeLayout相对于FrameLayout的内边距为状态栏的高度了 也就完美的达到了沉浸式状态栏的目的,而且保证导航栏的相对位置不变。
这里存在一个common_back作为一个基准来控制高度达到状态栏的高度,如果一个activity只有一个背景图片或者不以类似导航栏的空间作为基准的话,怎么实现沉浸式状态栏,例如达到这种状态 我们只需要在代码这样设置
protected void setImmerseLayout(View view) {
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
/*window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);*/
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
/* int statusBarHeight = ScreenUtil.getStatusBarHeight(this.getBaseContext());
view.setPadding(0, statusBarHeight, 0, 0);*/
然后在当前activity的布局文件中加入这两句话 android:fitsSystemWindows=true android:clipToPadding=true
这时候状态栏的背景与Activity的整个大背景融合在一起了
基于以上的方法介绍,我们可以实现状态栏与导航栏以及状态栏与页面大背景的沉浸式体验。
其实上面也可以看出代码封装的一些技巧:如让我们所有的activity继承BaseActivity,这样像
setImmerseLayout(findViewById(mon_back)); initBackButton(); setTitleBar(R.string.durian_about); 诸如此类的操作实现起来省时省力了!
相关搜索:
相关阅读:
相关频道:
Android教程最近更新

我要回帖

更多关于 android 沉浸式状态栏 的文章

 

随机推荐