Android 沉浸式状态栏

    一、隐藏状态栏

        // 隐藏标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 隐藏状态栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

二、Activity中设置View上拉到状态栏:
1.Activity配置清单
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.NoActionBar"
2.设置Activity样式:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>


  注:applicationTheme不能设置沉浸式状态栏属性
沉浸式状态栏为true
<item name="android:fitsSystemWindows">true</item>
<item name="android:clipToPadding">false</item>
三、沉浸式状态栏:

沉浸式状态栏:

       标题栏:Android手机左上最顶上处;


       状态栏:Android手机右上最顶上处;

  导航栏:手机最下面的,返回,Home,主页,三个按钮;

   1.  引入类库

       使用Android Studio,直接在build.gradle文件中引入库:

    dependencies {
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'

2.在setContentView之前设置:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);//true 将布局置顶,文字覆盖状态栏 false文字在状态栏的下面
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);//设置栏状态颜色,true默认使用setStatusBarTintResource颜色,否则为透明色
tintManager.setStatusBarTintResource(R.color.black);//通知栏所需颜色
}


@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}

注:setTranslucentStatus
 沉浸式状态栏都设置为true
* 1.true 布局覆盖状态栏
* setStatusBarTintEnabledtrue状态栏使用setStatusBarTintResource所设置的颜色
* setStatusBarTintEnabledfalse状态栏使用透明色;及无颜色值沉浸式状态栏
*
* 2.false 布局在状态栏下面
*setStatusBarTintEnabledtrue状态栏使用stylescolorPrimaryDark属性的颜色
* setStatusBarTintEnabledfalse状态栏使用stylescolorPrimaryDark属性的颜色

//设置状态栏颜色
 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
Window window = getActivity().getWindow();
// 设置透明状态栏,这样才能让 ContentView 向上
// window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

// 需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// 设置状态栏颜色
window.setStatusBarColor(Color.parseColor("#4CDBBC"));
}



      

  

原文地址:https://www.cnblogs.com/huihuizhang/p/6503476.html