Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開發:實作透明(沉浸式)通知欄

編輯:關於 Android 開發

Android 開發:實作透明(沉浸式)通知欄

這個特性是 Android 4.4 才支援的,至少需要 API 19 才能使用,也就是說若裝置系統版本低於 4.4,沉浸式通知欄便不會生效。以下介紹使用方法,其實非常簡單。

/**
   * 設定通知欄 這個方法在 onCreate() 中實作,如果加在父類別的 onCreate() 裡,所有繼承該父類別的畫面都會有沉浸通知欄。
   */

public void initSystemBar() {
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
          setTranslucentStatus(true);
          SystemBarTintManager tintManager = new SystemBarTintManager(this);
          tintManager.setStatusBarTintEnabled(true);
          tintManager.setStatusBarTintResource(R.color.red);
       }
   }

/**
   * 設定通知欄的狀態
   * @param on
   */
    @SuppressLint("InlinedApi")
    private void setTranslucentStatus(boolean on) {  
        Window win = this.getWindow();  
        WindowManager.LayoutParams winParams = win.getAttributes();  
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;  
        if (on) {  
            winParams.flags |= bits;  
        } else {  
            winParams.flags &= ~bits;  
        }  
        win.setAttributes(winParams);  
    }

最後在版面配置檔中加入:android:fitsSystemWindows="true",即可實現。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved