Android最简单的沉浸式状态栏,且不遮挡虚拟导航栏(兼容7.0)

沉浸式状态栏已经几乎是App中必备,反正我觉得看着会舒服一点。


抠鼻

1. 修改Styles

styles.xml
<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="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

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

</resources>

2. 创建values-v19 和values-v21

创建对应的styles.xml

v19\styles.xml
<resources>

    <style name="TranslucentTheme" parent="TranslucentTheme.Base">
        <item name="android:windowTranslucentStatus">true</item>
    </style>

</resources>
v21\styles.xml
<resources>
    <style name="TranslucentTheme" parent="TranslucentTheme.Base">
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    </style>
</resources>

3. 在BaseActivity中做点手脚

在onCreate中添加下面代码就好了

//如果android4.4以上的状态栏未半透明
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
    //透明状态栏
    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
}

这个是kotlin的代码

4. 创建ele_title_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="@dimen/toolBarHeight"
    android:background="@color/blue">

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolBarPaddingTop"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/iv_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/back2"
            android:layout_centerVertical="true"
            android:visibility="gone"
            android:layout_marginLeft="@dimen/tool_bar_title_margin_left"/>

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/tool_bar_title_margin_left"
            android:layout_centerVertical="true"
            android:text="标题"
            android:textColor="@color/white"
            android:textSize="@dimen/text_size_big"
            android:layout_toRightOf="@id/iv_back"/>

        <ImageView
            android:id="@+id/iv_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/icon_list"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="15dp"
            android:visibility="gone"/>

        <ImageView
            android:id="@+id/iv_setting"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/selector_title_icon_setting"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="15dp"
            android:visibility="gone"/>

    </RelativeLayout>

</LinearLayout>

主要留意第一个布局和第一个view的toolBarHeighttoolBarPaddingTop
如果是api19以下和以上的值都是不一样的

dimens.xml
<dimen name="toolBarPaddingTop">0dip</dimen>
<dimen name="toolBarHeight">50dip</dimen>
v19\dimens.xml
<dimen name="toolBarPaddingTop">25dip</dimen>
<dimen name="toolBarHeight">75dip</dimen>
v21\dimens.xml
<dimen name="toolBarPaddingTop">25dip</dimen>
<dimen name="toolBarHeight">75dip</dimen>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容