<item name="android:windowTranslucentStatus">true</item> 最低要求的api是level19
statusbar或者actionBar维持显示,但是不占位,悬浮于布局上;
android:fitsSystemWindows="true" 的作用是,如果activity的主题是android: windowsTranslucentStatus = " true",则statusBar和actionBar悬浮于布局之上,但是占位,和普通的根布局的子view很相似;同时statusBar是透明的。
请看下面几种组合:
一: 只改变actionBar
以下都是基于普通布局:
<RelativeLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00a381"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="haha"
android:textAlignment="center"
android:textColor="@color/colorAccent"
android:textSize="38sp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
① normal
主题:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
效果图
② noActionBar
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
效果图
二 : 只改变android:windowTranslucentStatus属性 要求api level最小是19。
作用: 只要statusBar或者ActionBar存在就会被悬浮起来,并且不占位。
① 修改style
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
布局:
<RelativeLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00a381"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="haha"
android:textAlignment="center"
android:textColor="@color/colorAccent"
android:textSize="38sp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
效果图:
② 在①的style主题基础上再布局文件的跟节点下添加 android:fitsSystemWindows="true" ; 则statusBar和actionBar悬浮但是占位,类似于作为根元素的字元素;
布局:
<RelativeLayout 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="#ad7d4c"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00a381"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="haha"
android:textAlignment="center"
android:textColor="@color/colorAccent"
android:textSize="38sp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
效果图:
啥是NavigationBar呢?
下图中的1为:StatusBar
下图中的2为:NavigationBar
可能有同学要问了?andorid:windowTranslucentStatus = “true” 要求api level最低是19,那么如何兼容低版本呢?
对于低于api level19 的版本,很抱歉你无法实现statusBar和actionBar的悬浮效果;
为了在api level大于等于19的版本上实现statusBar和actionBar的悬浮效果;你
你必须定义2套styles.xml文件:
在res目录下新建values-v19目录,在该目录下新建styles.xml文件