天天看點

android 以兩種方式設定沉浸式狀态欄

以兩種方式設定 沉浸式狀态欄

1,使用 StatusBarCompat 庫

//沉浸式狀态欄
api ('com.github.niorgai:StatusBarCompat:2.1.3', {
    exclude group:'com.android.support'
})
           

然後在activity 中 設定

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Latte.getConfigurator().withActivity(this);

    //隐藏ActionBar
    ActionBar actionBar = getSupportActionBar();
    if (actionBar!= null){
        actionBar.hide();
    }
    /**
     * 一句話 實作沉浸式狀态欄,
     */
    StatusBarCompat.translucentStatusBar(this,true);
}
           

2,使用代碼的方式來實作:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Latte.getConfigurator().withActivity(this);

    ActionBar actionBar = getSupportActionBar();
    if (actionBar!= null){
        actionBar.hide();
    }

    //當版本大于 5.0 時 實作沉浸式 狀态欄
    if (Build.VERSION.SDK_INT >= 21) {
        View decorView = getWindow().getDecorView();
        int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        decorView.setSystemUiVisibility(option);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
    }else {
    	//勉強适配 4.1 到5.0
        View decorView = getWindow().getDecorView();
        @SuppressLint("InlinedApi") int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(option);
    }
}
           

然後就可以自定義 toolbar ,使用 toolbar 有可能會出現 toolbar 和狀态欄重疊。解決方法為。

繼續閱讀