android沉浸模式
❶ 安卓開發中怎樣設置沉浸式狀態欄
這個特性是andorid4.4支持的,最少要api19才可以使用。下面介紹一下使用的方法,非常得簡單:
   
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //透明狀態欄
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明導航欄
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
 
    }
}
   
//透明狀態欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明導航欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
只要加入這兩行代碼,就可以實現沉浸式通知欄了。
給大家看看這個界面的布局:
    
<linearlayout android:background="#ffffff" android:cliptopadding="true" android:fitssystemwindows="true" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<textview android:background="#009959" android:layout_height="100dp" android:layout_width="match_parent"><button android:background="#ff669d/" android:layout_height="50dp" android:layout_width="100dp"></button></textview></linearlayout>
大家看紅色的那部分,加入那兩行以後,界面仍然會是沉浸式的,但狀態欄那部分,就不會再重疊了,像加了padding一樣,如下圖:
大家看圖,綠色的textView和紅色的一個button都被下移了,狀態欄是白色的,是背景linearLayout的顏色。很明顯,這也不是我們想要的,我們希望狀態欄和我們放在頂部的控制項是同一個顏色,同時,控制項內容也不和狀態欄重復,其實,只要把那兩行代碼放到我們頂部的控制項就可以了。代碼如下:
<linearlayout android:background="#ffffff" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<textview android:background="#009959" android:cliptopadding="true" android:fitssystemwindows="true" android:layout_height="100dp" android:layout_width="match_parent" android:text="你好,請問你有男朋友嗎/"><button android:background="#ff669d/" android:layout_height="50dp" android:layout_width="100dp"></button></textview></linearlayout>
就是那兩行紅色的代碼,放在綠色的textView上,這樣,就會是下面的效果:
 
這就是我們想要的了。
❷ 如何實現Android沉浸式狀態欄
Android在4.4的時候增加了透明狀態欄與導航欄的功能,依託於這個新特性,我們可以開始跟隨潮流,實現Android的沉浸式狀態欄 其實上圖展示的這個關於界面的代碼非常簡單/**
 * 關於界面
 *
 * @author SuS
 * @time 2015.07.29
 */
public class AboutActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_about);
        setImmerseLayout(findViewById(R.id.common_back));
        initBackButton();
        setTitleBar(R.string.rian_about);
    }
    @Override
    protected void onResume() {
        super.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
現在請注意setImmerseLayout()這個方法,這個方法是在BaseActivity中實現的public class BaseActivity extends FragmentActivity {
    private static final String TAG = BaseActivity;
  ...............
   
    public void initBackButton() {
        ImageView backButton = (ImageView) this.findViewById(R.id.rian_back_image);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishActivity();
            }
        });
    }
   
    protected void setImmerseLayout(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                Window window = getWindow();
                /*window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);*/
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            int statusBarHeight = ScreenUtil.getStatusBarHeight(this.getBaseContext());
            view.setPadding(0, statusBarHeight, 0, 0);
        }
    }
   
    public void finishActivity() {
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }
   
    public void setTitleBar(int id) {
        TextView tvName = (TextView) findViewById(R.id.rian_title_text);
        tvName.setText(id);
    }
}
使用 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 或者 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
都可以使狀態欄透明,但是關鍵是下面這兩行代碼,如果沒有這兩行,會是這樣 
那麼這兩行神奇的代碼的原理是什麼? 我們先看一下ScreenUtil中的getStatusBarHeight方法  /**
     * 用於獲取狀態欄的高度。 使用Resource對象獲取(推薦這種方式)
     *
     * @return 返回狀態欄高度的像素值。
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier(status_bar_height, dimen,
                android);
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
這里是獲得狀態欄的高度,然後我們就可以通過設置common_back的padding屬性 即:view.setPadding(0, statusBarHeight, 0, 0)來達到終極效果 但是這里還是需要注意細節的,首先大家應該理解padding的含義:內邊距 那麼再看一下common_back的布局文件 在activity_about.xml中我們是使用include引用的common_back
common_back的布局如下:
<framelayout android:background="@color/common_top_bg" android:id="@+id/rian_head_layout" android:layout_height="wrap_content" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
</framelayout>
這里我們使用了一層FrameLayout包裹住RelativeLayout,這里有什麼玄機那,其實這里就是為了 方便上面那兩行神奇的代碼起作用,這樣我們就能設置RelativeLayout相對於FrameLayout的內邊距為狀態欄的高度了 也就完美的達到了沉浸式狀態欄的目的,而且保證導航欄的相對位置不變。
這里存在一個common_back作為一個基準來控制高度達到狀態欄的高度,如果一個activity只有一個背景圖片或者不以類似導航欄的空間作為基準的話,怎麼實現沉浸式狀態欄,例如達到這種狀態 我們只需要在代碼這樣設置   protected void setImmerseLayout(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                Window window = getWindow();
                /*window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);*/
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
           /* int statusBarHeight = ScreenUtil.getStatusBarHeight(this.getBaseContext());
            view.setPadding(0, statusBarHeight, 0, 0);*/
        }
    }
然後在當前activity的布局文件中加入這兩句話 android:fitsSystemWindows=true android:clipToPadding=true
這時候狀態欄的背景與Activity的整個大背景融合在一起了 
總結:
基於以上的方法介紹,我們可以實現狀態欄與導航欄以及狀態欄與頁面大背景的沉浸式體驗。
其實上面也可以看出代碼封裝的一些技巧:如讓我們所有的activity繼承BaseActivity,這樣像
 
setImmerseLayout(findViewById(R.id.common_back)); initBackButton(); setTitleBar(R.string.rian_about); 諸如此類的操作實現起來省時省力了!
❸ Android QQ沉浸式狀態欄效果是怎麼實現的
首先是兩個開啟沉浸模式和關閉沉浸模式的函數
?
@SuppressLint("NewApi")
public static void hideSystemUI(View view) {
 view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
 | View.SYSTEM_UI_FLAG_FULLSCREEN
 | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
} 
@SuppressLint("NewApi")
public static void showSystemUI(View view) {
 view.setSystemUiVisibility(
 View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
  | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
這些代碼可以在google的開發者文檔中找到,可以看這里Using Immersive Full-Screen Mode,上面的代碼是在Android 4.4中才會生效,對應的Android版本兼容的判斷請自行處理。
此外還需要一個輔助函數,用於獲得狀態欄高度,使用反射獲得。
?
/**
 * 獲狀態欄高度
 *
 * @param context 上下文
 * @return 通知欄高度
 */
public int getStatusBarHeight(Context context) {
 int statusBarHeight = 0;
 try {
 Class<?> clazz = Class.forName("com.android.internal.R$dimen");
 Object obj = clazz.newInstance();
 Field field = clazz.getField("status_bar_height");
 int temp = Integer.parseInt(field.get(obj).toString());
 statusBarHeight = context.getResources().getDimensionPixelSize(temp);
 } catch (Exception e) {
 e.printStackTrace();
 }
 return statusBarHeight;
}  
點擊hide按鈕進入沉浸模式,也就是隱藏狀態欄,隱藏狀態欄的同時需要修改Toolbar的上內邊距,否則會顯得很難看,這里注冊一個監聽,當進入沉浸模式後咱們改變Toolbar的上內邊距
?
hide.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 View view = getWindow().getDecorView();
 hideSystemUI(view);
 mToolbar.set(new View.() {
 @Override
 public void onSystemUiVisibilityChange(int visibility) {
 mToolbar.setPadding(mToolbar.getPaddingLeft(), 0,mToolbar.getPaddingRight(), mToolbar.getPaddingBottom());
 }
 });
 }
});
進入沉浸模式後,手指從屏幕頂部向下劃,狀態欄就出現了,過2秒左右它又會自動消失。
點擊show按鈕退出沉浸模式,同時Toolbar的內邊距也要增加到狀態欄的高度。
?
show.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 View view = getWindow().getDecorView();
 showSystemUI(view);
 mToolbar.set(new View.() {
 @Override
 public void onSystemUiVisibilityChange(int visibility) {
 mToolbar.setPadding(mToolbar.getPaddingLeft(), getStatusBarHeight(MainActivity.this),mToolbar.getPaddingRight(), mToolbar.getPaddingBottom());
 }
 });
 }
});
如果使用的是SystemBarTintManager這個類進行的狀態欄的著色,除上方的操作外,還要在對應的監聽里增加狀態欄著色的禁止和啟動的功能。
進入沉浸模式,要禁用
?   
tintManager.setStatusBarTintEnabled(false);
退出沉浸模式,要啟動
?
tintManager.setStatusBarTintEnabled(true);  
如果想更加平滑,則可以對padding的改成增加動畫,具體動畫效果自行添加。
❹ 為什麼國內Android應用都不適配沉浸式狀態欄
- 簡單介紹 - 沉浸式是APP界面圖片延伸到狀態欄, 應用本身沉浸於狀態欄。當啟用該模式,應用程序的界面將占據整個屏幕,系統自動將隱藏系統的狀態欄和導航欄,讓應用程序內容可以在最大顯示範圍呈現。 
- 常見問題 
- 4.4及其以上都是可以實現沉浸式狀態欄效果的,5.0及其以上可以直接在主題中設置顏色,也可以調用Window類中的setStatusBarColor(int color)來實現,這兩種方式在5.0上都比較簡單。 
- 圖片背景的頁面讓狀態欄透明及半透明。  - ❺ 如何實現Android沉浸式狀態欄 - 注意引入相關依賴: 
 Java代碼
 compile 'com.android.support:appcompat-v7:22.2.1'
 compile 'com.android.support:support-v4:22.2.1'
 compile 'com.android.support:design:22.2.0'
 (一)colors.xml 和 styles.xml
 首先我們定義幾個顏色:
 res/values/color.xml
 XML/HTML代碼
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
 <color name="primary">#FF03A9F4</color>
 <color name="primary_dark">#FF0288D1</color>
 <color name="status_bar_color">@color/primary_dark</color>
 </resources>
 下面定義幾個styles.xml
 注意文件夾的路徑:
 values/styles.xml
 XML/HTML代碼
 <resources>
 <style name="BaseAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
 <!-- Customize your theme here. -->
 <item name="colorPrimary">@color/primary</item>
 <item name="colorPrimaryDark">@color/primary_dark</item>
 <item name="colorAccent">#FF4081</item>
 </style>
 
 <!-- Base application theme. -->
 <style name="AppTheme" parent="@style/BaseAppTheme">
 </style>
 </resources>
 values-v19
 XML/HTML代碼
 <resources>
 <style name="AppTheme" parent="@style/BaseAppTheme">
 <item name="android:windowTranslucentStatus">true</item>
 </style>
 </resources>
 ok,這個沒撒說的。注意我們的主題是基於NoActionBar的,android:windowTranslucentStatus這個屬性是v19開始引入的。
 (二)布局文件
 activity_main.xml
 XML/HTML代碼
 <android.support.v4.widget.DrawerLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 >
 <LinearLayout
 android:id="@+id/id_main_content"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 
 <android.support.v7.widget.Toolbar
 android:id="@+id/id_toolbar"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="?attr/colorPrimary"
 android:fitsSystemWindows="true"
 app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
 <TextView
 android:id="@+id/id_tv_content"
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="1"
 android:gravity="center"
 android:text="HelloWorld"
 android:textSize="30sp"/>
 </LinearLayout>
 <android.support.design.widget.NavigationView
 android:id="@+id/id_nv_menu"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_gravity="start"
 android:fitsSystemWindows="true"
 app:headerLayout="@layout/header_just_username"
 app:menu="@menu/menu_drawer"
 />
 </android.support.v4.widget.DrawerLayout>
 DrawerLayout內部一個LinearLayout作為內容區域,一個NavigationView作為菜單。
 注意下Toolbar的高度設置為wrap_content。
 然後我們的NavigationView中又依賴一個布局文件和一個menu的文件。
 header_just_username.xml
 XML/HTML代碼
 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="192dp"
 android:background="?attr/colorPrimaryDark"
 android:orientation="vertical"
 android:padding="16dp"
 android:fitsSystemWindows="true"
 android:theme="@style/ThemeOverlay.AppCompat.Dark">
 
 <TextView
 android:id="@+id/id_link"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_marginBottom="16dp"
 android:text="http://blog.csdn.net/lmj623565791"/>
 
 <TextView
 android:id="@+id/id_username"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@id/id_link"
 android:text="Zhang Hongyang"/>
 
 <ImageView
 android:layout_width="72dp"
 android:layout_height="72dp"
 android:layout_above="@id/id_username"
 android:layout_marginBottom="16dp"
 android:src="@mipmap/ic_launcher"/>
 </RelativeLayout>
 menu的文件就不貼了,更加詳細的可以去參考Android 自己實現 NavigationView [Design Support Library(1)]。
 大體看完布局文件以後,有幾個點要特別注意:
 • ToolBar高度設置為wrap_content
 • ToolBar添加屬性android:fitsSystemWindows="true"
 • header_just_username.xml的跟布局RelativeLayout,添加屬性android:fitsSystemWindows="true"
 android:fitsSystemWindows這個屬性,主要是通過調整當前設置這個屬性的view的padding去為我們的status_bar留下空間。
 根據上面的解釋,如果你不寫,那麼狀態欄和Toolbar就會有擠一塊的感覺了,類似會這樣。- ❻ android中在多個Fragment中切換是否沉浸,界面布局不能鋪滿或者被拉伸 - 天想試試沉浸欄的使用,但是按照網上相應的方法設置完成後,沒有達到想要的結果。使用情況是activity配合groupradio實現fragment切換,每個fragment的狀態欄需要顯示不同的顏色。通過hide和show的方式控制顯示當前fragment。在對應的xml中設置Android:fitsSystemWindows="true",以達到改變狀態欄顏色的問題(具體如何配置請查閱其他文檔)。 - 但是這樣做第一個fragment達到了想要的效果,其他三個狀態欄的顏色正確,但是padding沒有設置成功。標題欄佔用狀態欄的位置。 - 解決方法: - 將xml中的android:fitsSystemWindows="true"去掉,通過代碼的方式來控制。在默認第一個fragment的onCreateView中設置view.setFitsSystemWindows(true);該view為該fragment對應的view。在onHiddenChanged中,設置對應的狀態。 - 詳細 - ❼ 如何實現Android沉浸式狀態欄 - styles.xml設置如下: 
 <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
 <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
 <style name="AppTheme.NoActionBar">
 <item name="windowActionBar">false</item>
 <item name="windowNoTitle">true</item>
 <item name="windowActionModeOverlay">true</item>
 <item name="android:actionModeBackground">@drawable/context_menu</item>
 </style>
 <style name="TranslucentTheme" parent="AppTheme.NoActionBar">
 </style>
 V21的styles.xml設置如下:
 <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
 <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
 <style name="AppTheme.NoActionBar">
 <item name="windowActionBar">false</item>
 <item name="windowNoTitle">true</item>
 <item name="android:">true</item>
 <item name="android:windowContentTransitions">true</item>
 <item name="android:statusBarColor">@color/colorPrimary</item>
 <item name="windowActionModeOverlay">true</item>
 <item name="android:actionModeBackground">@drawable/context_menu</item>
 </style>
 <style name="TranslucentTheme" parent="AppTheme.NoActionBar">
 <item name="android:windowTranslucentStatus">false</item>
 <item name="android:windowTranslucentNavigation">false</item>
 </style>
 再在要顯示的toolbar里加上屬性:
 android:fitsSystemWindows="true"
 主題的屬性設置為:
 <style name="TranslucentTheme" parent="AppTheme.NoActionBar">- ❽ 如何實現Android沉浸式狀態欄 - 沉浸式通知欄Android4.4以上才支持的新特性。4.3不支持。 
 具體實現方式如下:
 1.新建個公共style,設置android:fitsSystemWindows=true
 <!-- 設置應用布局時是否考慮系統窗口布局;true --> <style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar"> <item name="android:fitsSystemWindows">true</item> </style>
 2. 修改AndroidManifest.xml,讓所有的activity樣式默認設置為AppBaseTheme(*不同項目要靈活處理,筆者項目的activity樣式都是統一的所以這樣設置沒問題,但是實際情況下不同的activity可能調用的樣式不一樣,需要讀者自行按自己的項目來設置)
 <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppBaseTheme" android:name="****">
 3.新增沉浸式通知欄實現類,實現原理很簡單。
 1)判斷當前系統版本是不是4.4以上,判斷代碼如下:
 if (VERSION.SDK_INT >= VERSION_CODES.KITKAT)
 2)如果大於4.4則設置狀態欄透明化,代碼如下:
 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
 3)獲取activity的根rootView(DecorView),然後創建一個新的view stateBarView並把它添加到rootView(這裏手動給它設置個ID,下次進來時先判斷rootView是否已創建stateBarView,如果已創建則直接獲取該View這樣可以防止重復創建,導致內存泄露)
 以下是具體代碼實現:
 import android.annotation.SuppressLint;import android.app.Activity;import android.content.res.Resources;import android.graphics.drawable.Drawable;import android.os.Build;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.widget.FrameLayout.LayoutParams;/** * 沉浸式通知欄公共類 * @author hurrican * */@SuppressLint({ "InlinedApi", "ResourceAsColor" })public class ImmersedNotificationBar { private Activity activity ; //設置沉浸式通知欄的ID(防止重復創建) private final static int IMMERSED_NOTIFICATION_BAR_ID = 12345678 ; private final static String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height" ; public ImmersedNotificationBar(Activity activity){ this.activity = activity ; } //獲取狀態欄高度 private int getStatusBarHeight(Resources res){ int statusBarHeight = 0; int resourceId = res.getIdentifier(STATUS_BAR_HEIGHT_RES_NAME, "dimen", "android"); if (resourceId > 0) { statusBarHeight = res.getDimensionPixelSize(resourceId); } return statusBarHeight ; } //添加頂部狀態欄 private View addStateBar(Activity activity,ViewGroup rootView,int statusBarHeight){ //創建新的View,並添加到rootView頂部) View statusBarView ; if(null!=rootView.findViewById(IMMERSED_NOTIFICATION_BAR_ID)){ statusBarView = rootView.findViewById(IMMERSED_NOTIFICATION_BAR_ID); }else{ statusBarView = new View(activity); rootView.addView(statusBarView); } statusBarView.setId(IMMERSED_NOTIFICATION_BAR_ID) ; LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,statusBarHeight); params.gravity = Gravity.TOP; statusBarView.setLayoutParams(params); statusBarView.setVisibility(View.VISIBLE); return statusBarView ; } /** * 設置狀態欄顏色 * @param ColorId */ public void setStateBarColor(int ColorId){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = activity.getWindow(); //activity的頂級布局 ViewGroup rootView = (ViewGroup) window.getDecorView(); //透明化狀態欄 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Resources res = activity.getResources(); //獲取狀態欄目的高度 int statusBarHeight = getStatusBarHeight(res); View stateBarView = addStateBar(activity,rootView,statusBarHeight) ; stateBarView.setBackgroundColor(ColorId) ; } } /** * 設置狀態欄顏色 * @param ColorId */ public void setStateBarDrawable(Drawable drawable){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = activity.getWindow(); //activity的頂級布局 ViewGroup rootView = (ViewGroup) window.getDecorView(); //透明化狀態欄 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Resources res = activity.getResources(); //獲取狀態欄目的高度 int statusBarHeight = getStatusBarHeight(res); View stateBarView = addStateBar(activity,rootView,statusBarHeight) ; stateBarView.setBackgroundDrawable(drawable) ; } }}- ❾ 如何實現Android沉浸式狀態欄 - styles.xml設置如下: <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/> <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/> <style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="windowActionModeOverlay">true</item> <item name="android:actionModeBackground">@drawable/context_menu</item> </style> <style name="TranslucentTheme" parent="AppTheme.NoActionBar"> </style> V21的styles.xml設置如下: <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> <style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="android:">true</item> <item name="android:windowContentTransitions">true</item> <item name="android:statusBarColor">@color/colorPrimary</item> <item name="windowActionModeOverlay">true</item> <item name="android:actionModeBackground">@drawable/context_menu</item> </style> <style name="TranslucentTheme" parent="AppTheme.NoActionBar"> <item name="android:windowTranslucentStatus">false</item> <item name="android:windowTranslucentNavigation">false</item> </style> 再在要顯示的toolbar里加上屬性: android:fitsSystemWindows="true" 主題的屬性設置為: <style name="TranslucentTheme" parent="AppTheme.NoActionBar"> - ❿ 如何實現Android沉浸式狀態欄 - 通知欄(notification)背景顏色跟隨app導航欄(top title)背景顏色變化而變化。 
 這也叫沉浸式狀態欄,這是Google在Android 4.4以上的系統提出的。
 在activity中加入:
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
 }
 然後在xml布局文件中加入:
 android:clipToPadding="false"
 android:fitsSystemWindows="true"
 有什麼不會的再問我。

