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>