Android沉浸式狀態(tài)欄

此文章記錄怎樣實(shí)現(xiàn)Android沉浸式狀態(tài)欄

方法一:也是最簡(jiǎn)單的方法,在style文件中做如下配置

<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <!--Android 5.x開(kāi)始需要把顏色設(shè)置透明,否則導(dǎo)航欄會(huì)呈現(xiàn)系統(tǒng)默認(rèn)的淺灰色-->
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

在對(duì)應(yīng)的activity中引入屬性即可:

<activity android:name=".MainActivity"
          android:theme="@style/TranslucentTheme"/>

方法二:
1)、獲取actionBar,并將actionBar隱藏 ,注意兩種方法對(duì)應(yīng)不同版本,根據(jù)版本選擇其中一種。

       ActionBar supportActionBar = getSupportActionBar();
       android.app.ActionBar actionBar = getActionBar();
       supportActionBar.hide();

隱藏actionBar還有一種方法

requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

在setContentView(R.layout.activity_main)前調(diào)用requestWindowFeature,注意經(jīng)試驗(yàn)此方法只是在當(dāng)前Activity繼承自Activity才可以生效,繼承自AppCompatActivity不會(huì)生效。
2)、通過(guò)反射獲取狀態(tài)欄的高度。

/**
     * 通過(guò)反射獲取狀態(tài)欄高度
     * @return
     */
    private int getStatusBarHeight(){
        try {
            //通過(guò)反射獲取到類
            Class<?> aClass = Class.forName("com.android.internal.R$dimen");
            //創(chuàng)建對(duì)象
            Object o = aClass.newInstance();
            //獲取對(duì)應(yīng)屬性
            Field status_bar_height = aClass.getField("status_bar_height");
            //獲取對(duì)應(yīng)屬性的值
            Object o1 = status_bar_height.get(o);
            int height = Integer.parseInt(o1.toString());
            //返回值
            return getResources().getDimensionPixelOffset(height);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return 0;
    }

3)、將狀態(tài)欄設(shè)置為透明,并將下面的布局高度延伸至導(dǎo)航欄高度

/**
     * 系統(tǒng)版本4.4以上才可以設(shè)置沉浸式狀態(tài)欄
     */
    private void setStatus(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            //設(shè)置狀態(tài)欄透明
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            mTextView=findViewById(R.id.my_text);
            final int statusBarHeight = getStatusBarHeight();
            mTextView.post(new Runnable() {
                @Override
                public void run() {
                    int height = mTextView.getHeight();
                    ViewGroup.LayoutParams layoutParams = mTextView.getLayoutParams();
                    layoutParams.height = height+statusBarHeight;
                    mTextView.setLayoutParams(layoutParams);
                }
            });
        }
    }

以上兩種方法僅為個(gè)人記錄整理,歡迎大家指出不足和可優(yōu)化的地方。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容