主題使用:
- 使用Theme.AppCompat.Light.NoActionBar(toolbar的兼容主題)
關于fitsSystemWindows 屬性介紹:
- 如果你希望拓展的區(qū)域不被狀態(tài)欄遮擋住使用:fitsSystemWindows屬性
- fitsSystemWindows 按照深度優(yōu)先的方式其作用孽拷,所以使用該屬性的 View 的順序是有要求的怀喉,如果第一個 View 使用了 inset (系統(tǒng)窗口的尺寸)則會導致其他 View 尺寸不一樣。
- Inset 總是相對于全屏幕的获高,Inset 可能在 View layout 之前就已經(jīng)應用了哈肖,所以在設置 View 的 padding 之前 View 并不知道其具體相對于系統(tǒng)窗口的位置。
- View 的其他 padding 值被重新改寫了念秧,在使用 fitsSystemWindows 為 true 的View 上設置 padding 值(paddingLeft/paddingTop/ 等)是沒有效果的淤井。
- 比如 你想把 RecyclerView 的內(nèi)容顯示在一個透明導航欄的下面,就類似于 Google Now 一樣摊趾,你可以在 RecyclerView 上設置 android:fitsSystemWindows=”true” 币狠,然后在設置 RecyclerView 的 android:clipToPadding=”false”,這樣這個 RecyclerView 就會顯示在導航欄下方了严就,當你向上滑動 RecyclerView 到底的時候, RecyclerView 內(nèi)容在導航欄上方器罐,并沒有被導航欄擋住梢为。
- 使用fitSystemWindows屬性讓系統(tǒng)幫我們自動適配不同情況下的status bar,讓我們的view的paddingTop獲取到一個合理的值轰坊。(還有其他的方案是通過手動設置paddingTop的值來進行適配的:在values-v19里設置paddingTop值為25dp铸董,在values里設置為0dp,但是在某些自定義的rom里status bar的高度是被有修改過的。還有就是通過自定義繼承toolbar肴沫,在代碼里動態(tài)獲取status bar的高度并設置paddingTop的值粟害,但這樣又弄得太麻煩了)。
code
BaseActivity
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//系統(tǒng)版本的校驗
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
/**
* 使用:FLAG_TRANSLUCENT_STATUS標志位颤芬,activity布局會擴展到狀態(tài)欄
*
*/
//設置參數(shù)悲幅,將status設置成透明
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//添加statusbar 透明
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);// 在存在虛擬按鍵的手機上,導航欄為透明
}
}
}
MainActivity
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 自定義toolbar的時候可以將下面注釋掉站蝠,
* 在系統(tǒng)布局中使用colorPrimary和minHeight指定顏色和高度
*/
//將toolbar設置成actionbar
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
}
}
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.leiiiooo.immersivedemo.MainActivity">
<!--toolbar-->
<!--<include
layout="@layout/mytoolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />-->
<!--自定義topbar-->
<include
layout="@layout/mytopbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="ThinkCool" />
</LinearLayout>
mytoolbar_layout.xml
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
mytopbar_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:gravity="center"
android:fitsSystemWindows="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/top_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/top_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="登陸"
android:textSize="16sp" />
<ImageView
android:id="@+id/top_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
</LinearLayout>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.leiiiooo.immersivedemo">
<!--設置成沒有actionbar的主題-->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".BaseActivity">
</activity>
</application>
</manifest>
效果如圖:
使用系統(tǒng)toolbar
自定義toolbar+fitsSystemWindows
自定義toolbar 未設置fitsSystemWindows