沉浸式状态栏

2019-03-08  本文已影响0人  just0119

android Api21之后的状态栏会默认覆盖半透明遮罩,我们需要新建values-19,values-21 2份styles文件进行适配

Api-19
 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
 </style>
Api-21
 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
 </style>
02dcb82d382be8e958a4245dee2465d.jpg
假如需要去除状态栏需要在Activity中添加
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
d8b9a347144e379b5c86f80fe716466.jpg
这种情况下,布局内容会延伸到状态栏,假如我们需要状态栏显示需要另外做处理
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:statusBarColor">#ff0000</item>
</style>
// 或者通过代码
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().setStatusBarColor(Color.BLUE);
private void addStatusBarColor() {
    ViewGroup mViewGroup = findViewById(android.R.id.content);
    View mStatusView = new View(this);
    ViewGroup.LayoutParams mParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight());
    mStatusView.setBackgroundColor(Color.BLUE);
    mViewGroup.addView(mStatusView,mParams);
}

/**
 * 通过反射拿到状态栏高度
 */
private int getStatusBarHeight() {
    int height = 0;
    int resId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if(resId > 0){
       height = getResources().getDimensionPixelSize(resId);
    }
    return height;
}
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    getWindow().setStatusBarColor(Color.BLUE);
} else if(Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT){
    ViewGroup mViewGroup = findViewById(android.R.id.content);
    View mStatusView = new View(this);
    ViewGroup.LayoutParams mParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight());
    mStatusView.setBackgroundColor(Color.BLUE);
    mViewGroup.addView(mStatusView,mParams);
}

以上都需要添加在布局中添加fitsSystemWindow = true ,或者在代码中进行设置

public void setRootViewFitsSystemWindows() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        ViewGroup winContent = findViewById(android.R.id.content);
        if (winContent.getChildCount() > 0) {
            ViewGroup rootView = (ViewGroup) winContent.getChildAt(0);
            if (rootView != null) {
                rootView.setFitsSystemWindows(true);
            }
        }
    }
}
9ba64c82d2450d873fccc6c2545b883.jpg
// 代码设置
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
// 新建values-v23文件夹
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">#fff</item>
    <!-- Android 6.0以上 状态栏字色和图标为浅黑色-->
    <item name="android:windowLightStatusBar">true</item>
</style>
899a9f36f7e795e0808aab24c324fb3.jpg
6a5446429a3ed4ea81c429711ac21b9.jpg
上一篇下一篇

猜你喜欢

热点阅读