使用SystemBarTint搭配NavigationView在Android4.4以上實(shí)現(xiàn)透明狀態(tài)欄

勇敢的少年啊,快來看看什么叫做抽屜導(dǎo)航之美吧~

女神鎮(zhèn)樓

前言

在Android中,很多App的主界面都使用了抽屜式導(dǎo)航,比如網(wǎng)易云音樂、Fuubo、Bilibili等,就我個(gè)人而言,我是非常喜歡這種導(dǎo)航方式的,順便吐槽一下新版知乎,導(dǎo)航模式改為了底部導(dǎo)航,還取消了滑動(dòng)返回,用的心累。

抽屜式導(dǎo)航

這是官方給出的示例圖片,我覺得非常的漂亮,尤其是抽屜導(dǎo)航在狀態(tài)欄的下方,當(dāng)滑動(dòng)抽屜的時(shí)候可以很明顯的看出狀態(tài)欄與抽屜之間的層次關(guān)系,而這也是今天我們要實(shí)現(xiàn)的效果,以下示例都運(yùn)行在4.4版本上。

實(shí)現(xiàn)

首先看兩個(gè)比較關(guān)鍵的屬性,一個(gè)是:windowTranslucentStatus這個(gè)屬性支持的API最低為19,通常定義在values-v19/styles.xml下,它的作用是可以將狀態(tài)欄的顏色置為透明色,還有一個(gè)是fitsSystemWindows,這個(gè)屬性的作用是確保內(nèi)容不會(huì)顯示到系統(tǒng)窗口下面,這里的系統(tǒng)窗口指的是StatusBar和NavigationBar。

全透明狀態(tài)欄

首先看看網(wǎng)易云音樂在4.4上的表現(xiàn)

網(wǎng)易云音樂

可以看到抽屜是在狀態(tài)欄下面的,并且狀態(tài)欄是完全透明的,要實(shí)現(xiàn)這樣的效果也是很簡單的,使用Toolbar替換ActionBar,在values-v19/styles.xml和values-v21/styles.xml中定義windowTranslucentStatus為true。
代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="AppTheme.Base">
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>

activity_main代碼:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/toolbar" />

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="false"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/menu_drawer" />
</android.support.v4.widget.DrawerLayout>

要注意的是設(shè)置fitsSystemWindows為false。
其實(shí)到這里就已經(jīng)實(shí)現(xiàn)了這個(gè)效果,不過可以發(fā)現(xiàn)toolbar會(huì)向上移動(dòng),有一部分被狀態(tài)欄遮住,其實(shí)遮住的這一部分就是狀態(tài)欄的高度,因?yàn)樵O(shè)置為true會(huì),解決辦法是可以在Activity中為toolbar添加頂部的padding,代碼如下:

public class MainActivity extends AppCompatActivity {

    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setPadding(0,getStatusBarHeight(),0,0);
    }

    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}

最終效果:


著色狀態(tài)欄

這種抽屜方式,在滑動(dòng)的時(shí)候給人一種撕裂的感覺,看起來不是很舒服。

Bilibili

因?yàn)橐ostatusbar著色,所以要用到第三方庫SystemBarTint,但要注意這個(gè)庫已經(jīng)被作者標(biāo)記為DEPRECATED,即作者不贊成現(xiàn)在使用該庫,這個(gè)與今天主題無關(guān),就不多管了。
Android4.4(Kitkat)提出了透明狀態(tài)欄的概念,可以為status bar或是Navigation bar 設(shè)置透明度,而SystemBarTint的出現(xiàn)使我們更加方便為status bar和 Navigation bar著色,可以設(shè)置color或是Drawable。
首先必須允許status bar 透明,在values-v19/styles.xml中添加
<item name="android:windowTranslucentStatus">true</item>
activity_main代碼與上一個(gè)基本相同,只是不再需要fitsSystemWindows屬性。
Activity代碼:

public class MainActivity extends AppCompatActivity {

    private Toolbar mToolbar;
    private SystemBarTintManager mTintManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setPadding(0, getStatusBarHeight(), 0, 0);
        setTintManager();
    }

    @TargetApi(19)
    private void setTintManager() {
        mTintManager = new SystemBarTintManager(this);
        mTintManager.setStatusBarTintEnabled(true);
        mTintManager.setStatusBarTintColor(Color.parseColor("#2196F3"));
    }
    
    ...
}

最終效果:


帶透明度的狀態(tài)欄

這種其實(shí)就是實(shí)現(xiàn)官方給出的那種效果,也需要搭配SystemBarTint,代碼與相當(dāng)于是上面兩種的結(jié)合,只不過在設(shè)置statusbar顏色的時(shí)候設(shè)置的是#20000000,即使透明度為20%;

@TargetApi(19)
    private void setTintManager() {
        mTintManager = new SystemBarTintManager(this);
        mTintManager.setStatusBarTintEnabled(true);
        mTintManager.setStatusBarTintColor(Color.parseColor("#20000000"));
    }

最終效果:


參考

Android and the transparent status bar
該使用 fitsSystemWindows 了!

最后編輯于
?著作權(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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