android 全透明式状态栏

全透明状态栏

继上一篇博客fitsystemwindows简单使用之后发现5.0设置如下属性之后 状态栏是半透明的

 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
        //半透明头部状态栏  布局在状态栏下方
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
 }

在各种尝试之后,终于找到了下面这种方法,这个只试用于5.0

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }

Build.VERSION_CODES.LOLLIPOP:5.0
android 全透明式状态栏_第1张图片

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){//4.4 全透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 全透明实现
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);//calculateStatusColor(Color.WHITE, (int) alphaValue)
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.yqy.yqy_test.MainActivity">


    <LinearLayout  android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffe0a0" android:fitsSystemWindows="true" android:clipToPadding="true" android:gravity="center" android:orientation="horizontal">

        <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是标题"/>
    </LinearLayout>

    <TextView  android:layout_width="match_parent" android:layout_height="match_parent" android:text="我是内容" android:gravity="center" android:background="#e0ffa0"/>



</LinearLayout>

你可能感兴趣的:(android,沉浸式状态栏,着色式状态栏,全透明式状态栏)