android沉浸式状态栏

来源:互联网 发布:gitbash windows 编辑:程序博客网 时间:2024/06/08 10:53

第一种方式

第一种方式,需要做下面三步设置

1、在values、values-v19、values-v21的style.xml都设置一个 Translucent System Bar 风格的Theme

values/style.xml

<style name="ImageTranslucentTheme" parent="AppTheme">    <!--Android 4.4之前的版本上运行,直接跟随系统主题--></style>

values-v19/style.xml

<style name="ImageTranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">    <item name="android:windowTranslucentStatus">true</item>    <item name="android:windowTranslucentNavigation">true</item></style>

values-v21/style.xml

<style name="ImageTranslucentTheme" parent="Theme.AppCompat.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>

上面需要注意的地方是,无论你在哪个SDK版本的values目录下,设置了主题,都应该在最基本的values下设置一个同名的主题。这样才能确保你的app能够正常运行在 Android 4.4 以下的设备。否则,肯定会报找不到Theme的错误。
2、在AndroidManifest.xml中对指定Activity的theme进行设置

<activity    android:name=".ui.ImageTranslucentBarActivity"    android:label="@string/image_translucent_bar"    android:theme="@style/ImageTranslucentTheme" />

3、在Activity的布局文件中设置背景图片,同时,需要把android:fitsSystemWindows设置为true

activity_image_translucent_bar.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@mipmap/env_bg"    android:fitsSystemWindows="true"></RelativeLayout>

更多了解请参考:http://gold.xitu.io/entry/56b354e8c4c9710052e017aa/promote?utm_source=baidu&utm_medium=keyword&utm_content=android_statusbar&utm_campaign=q3_search

0 0
原创粉丝点击