1. android 沉浸式状态栏和透明状态栏的区别
注意!两种方法的区别:
第一种:为顶部栏跟随当前activity的布局文件的背景的颜色,使用方便,不过也有点问题就是,如果有底部虚拟导航键的话,导航键的背景跟顶部的颜色一样,比如:
第二种:是通过设置顶部栏的颜色来显示的,可以解决第一种的不足,比如:
第一种使用方法:
第一、首先在values、values-v19、values-v21文件夹下的styles.xml都设置一个 Translucent System Bar 风格的Theme,如下图:
values/style.xml:
<style name="TranslucentTheme" parent="AppTheme">
<!--在Android 4.4之前的版本上运行,直接跟随系统主题-->
</style>123
values-v19/style.xml:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>1234
values-v21/style.xml:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
<item name="android:statusBarColor">@android:color/transparent</item>
</style>123456
第二、在清单文件中配置需要沉浸式状态栏的activity加入theme
<activity android:name=".ImageActivity" android:theme="@style/TranslucentTheme" />
<activity android:name=".ColorActivity" android:theme="@style/TranslucentTheme" />12
第三、在Activity的布局文件中的跟布局加入“android:fitsSystemWindows=”true””,但是,这里需要区分一下,就是背景是图片还是纯色:
1.当背景为图片时,布局可以这么写:
<?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="@drawable/imgs_bj"
android:fitsSystemWindows="true">
</RelativeLayout>12345678
效果:
2.当背景为纯色,我们需要对布局划分一下,标题布局与内容布局,先把根布局背景设置成标题布局的背景色,然后标题背景色可以不用设置直接使用根布局的背景色,最后内容布局背景色设置为白色
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary" //根布局背景设置成“标题布局”想要的颜色
android:fitsSystemWindows="true"
android:orientation="vertical">
<!--标题布局-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@color/color_31c27c">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="这是标题"
android:textColor="@android:color/white"
android:textSize="20sp" />
</RelativeLayout>
<!--内容布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" //内容区域背景设置成白色
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_marginTop="120dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="显示信息"
android:onClick="showMsg"
/>
</LinearLayout>
</LinearLayout>
2. android沉浸式状态栏有一条半透明的底色怎么去掉
造成Android的SE已禁用有如下原因: 1.是自己刷机后没有wipe,双清。或手机在线ota升级后没有自动双清,导致系统缓存混乱,所以报错。 2.是用了某些同步工具,同步联系人,短信,通话记录,相册等导致的错乱问题。 3.SIM卡上存储的信息问题。 4.比如删除了系统必备apk,或者是替换了不同版本的apk,导致的错误。 建议按照以下方法操作: 1.手机中是否安装手机安全卫士软件或者一些第三方软件程序,如果有请卸载尝试。 2.建议把机器恢复出厂设置,操作如下:设置-重置/隐私权/私人-恢复出厂 3.建议固件升级尝试。(设置-关于设备-系统更新)
3. android导航栏与状态栏颜色及透明度
首先创建一个空项目,如下图
可以看到状态栏是白字黑背景, 导航栏也是白图标黑背景
嘿嘿, 我们先把状态栏隐藏掉,在添加一个ImageView, 让ImageView做背景(方便查看)
样子如下:
将状态栏和导航栏设置透明, 找到 Manifest.xml 文件, 在主题样式中修改
android:statusBarColor 设置状态栏背景色
android:navigationBarColor 同上
android:windowLightStatusBar 设置状态栏文字色, true为深色, false为白色
android:windowLightNavigationBar 同上
android:windowTranslucentStatus 设置状态栏半透明状态, true为半透明, false为不透明
android:windowTranslucentNavigation 同上
最后两个半透明状态下面没用, 可自己尝试看效果
效果图如下:
可以看到导航栏与状态栏并没有透明,原因是默认不能占用状态栏空间与导航栏空间,根布局背景为白色,所有这里显示白色
可以通过设置 getWindow().getDecorView().setSystemUiVisibility() 来适配
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 适配状态栏空间
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 适配导航栏空间
效果如下: