小米应用商店和内置的天气软件截图如下,我们可以看到顶部的通知栏和界面色调融为一体。这种bar称为Translucent System Bar
下面就对应实现这两种效果
第一种方式:
1、 在values、values-v19、values-v21的style.xml都设置一个 Translucent System Bar 风格的Theme
value/style.xml
<style name="ImageTranslucentTheme" parent="android:Theme.Light">
<!-- 在Android 4.4之前的版本上运行,直接跟随系统主题 -->
</style>
value-19/style.xml
<style name="ImageTranslucentTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
value-21/style.xml
<style name="ImageTranslucentTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<!-- Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色 -->
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
2、在AndroidManifest.xml中对指定Activity的theme进行设置
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/ImageTranslucentTheme" >
</activity>
3、在Activity的布局文件中设置背景图片,同时,需要把android:fitsSystemWindows设置为true
<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="@drawable/site"
android:fitsSystemWindows="true"
tools:context="${relativePackage}.${activityClass}" >
</RelativeLayout>
运行效果如下:
第二种方式:
1、其他设置不变,在Activity的布局文件中设置根布局背景颜色和标题栏的颜色
<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="#FF7B35"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#FF7B35" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="JYY"
android:textColor="@android:color/white"
android:textSize="25dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#F0F0F0" >
</RelativeLayout>
</LinearLayout>
运行效果如下: