Android最簡(jiǎn)單的沉浸式狀態(tài)欄,且不遮擋虛擬導(dǎo)航欄(兼容7.0)

沉浸式狀態(tài)欄已經(jīng)幾乎是App中必備,反正我覺得看著會(huì)舒服一點(diǎn)。


摳鼻

1. 修改Styles

styles.xml
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

    <style name="TranslucentTheme.Base" parent="AppTheme">
        <!--在Android 4.4之前的版本上運(yùn)行,直接跟隨系統(tǒng)主題-->
    </style>
    
    <style name="TranslucentTheme" parent="TranslucentTheme.Base">
        <!--在Android 4.4之前的版本上運(yùn)行,直接跟隨系統(tǒng)主題-->
    </style>

</resources>

2. 創(chuàng)建values-v19 和values-v21

創(chuàng)建對(duì)應(yīng)的styles.xml

v19\styles.xml
<resources>

    <style name="TranslucentTheme" parent="TranslucentTheme.Base">
        <item name="android:windowTranslucentStatus">true</item>
    </style>

</resources>
v21\styles.xml
<resources>
    <style name="TranslucentTheme" parent="TranslucentTheme.Base">
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    </style>
</resources>

3. 在BaseActivity中做點(diǎn)手腳

在onCreate中添加下面代碼就好了

//如果android4.4以上的狀態(tài)欄未半透明
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
    //透明狀態(tài)欄
    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
}

這個(gè)是kotlin的代碼

4. 創(chuàng)建ele_title_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="@dimen/toolBarHeight"
    android:background="@color/blue">

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolBarPaddingTop"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/iv_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/back2"
            android:layout_centerVertical="true"
            android:visibility="gone"
            android:layout_marginLeft="@dimen/tool_bar_title_margin_left"/>

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/tool_bar_title_margin_left"
            android:layout_centerVertical="true"
            android:text="標(biāo)題"
            android:textColor="@color/white"
            android:textSize="@dimen/text_size_big"
            android:layout_toRightOf="@id/iv_back"/>

        <ImageView
            android:id="@+id/iv_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/icon_list"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="15dp"
            android:visibility="gone"/>

        <ImageView
            android:id="@+id/iv_setting"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/selector_title_icon_setting"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="15dp"
            android:visibility="gone"/>

    </RelativeLayout>

</LinearLayout>

主要留意第一個(gè)布局和第一個(gè)view的toolBarHeighttoolBarPaddingTop
如果是api19以下和以上的值都是不一樣的

dimens.xml
<dimen name="toolBarPaddingTop">0dip</dimen>
<dimen name="toolBarHeight">50dip</dimen>
v19\dimens.xml
<dimen name="toolBarPaddingTop">25dip</dimen>
<dimen name="toolBarHeight">75dip</dimen>
v21\dimens.xml
<dimen name="toolBarPaddingTop">25dip</dimen>
<dimen name="toolBarHeight">75dip</dimen>
最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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