Android 沉浸式狀態欄(像IOS那樣的狀態欄與應用統一顏色樣式)java
注意:這個特性是Andorid4.4支持的,最少要API19纔可使用。android
下面介紹一下使用的方法,很是得簡單:佈局
1:在Activity的onCreate()方法中添加以下代碼:code
//透明狀態欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導航欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
2.在Activity的佈局文件中添加以下代碼:xml
android:fitsSystemWindows="true" android:clipToPadding="true"
例如:ip
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:clipToPadding="true" android:background="#ffffff" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:background="#009959" /> </LinearLayout>
3.如若狀態欄是白色的,將上面兩行屬性代碼放在xml中首層級下的第一個子控件佈局中。具體以下:get
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:fitsSystemWindows="true" android:clipToPadding="true" android:layout_width="match_parent" android:layout_height="100dp" android:background="#009959" android:text="你好,請問你有男友嗎"/> </LinearLayout>