沉浸式狀態欄

沉浸式狀態欄,網上方法是千奇百怪

我也總結了一下,實用爲主,沒有花裏胡哨,複製就可用

1,Theme方式

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <!-- 狀態欄透明設置 必須是4.4以上的版本-->
        <item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>
        <!-- 虛擬按鍵透明設置 -->
        <item name="android:windowTranslucentNavigation" tools:targetApi="kitkat">true</item>
        <!-- 5.0以上設置狀態欄的顏色  但是必須是windowTranslucentStatus爲false  -->
        <!--<item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent</item>-->
    </style>

2,代碼方式,放到setContentView前後都行

 private void initStatus() {
        //版本大於等於4.4
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            //獲取到狀態欄設置的兩條屬性
            int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
            //在4.4之後又有兩種情況  第一種 4.4-5.0   第二種 5.0以上
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
                //第二種 5.0以上
                Window window   = getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.flags |= flagTranslucentNavigation;
                window.setAttributes(attributes);
                window.setStatusBarColor(0);
            }else{
                //第一種 4.4-5.0
                Window window   = getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.flags |= flagTranslucentStatus|flagTranslucentNavigation;
                window.setAttributes(attributes);
            }
        }
     }
發佈了23 篇原創文章 · 獲贊 12 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章