美文网首页
Android最简单的沉浸式状态栏,且不遮挡虚拟导航栏(兼容7.

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

作者: 欢乐的乐 | 来源:发表于2017-07-27 14:28 被阅读1030次

    沉浸式状态栏已经几乎是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>
    

    相关文章

      网友评论

          本文标题:Android最简单的沉浸式状态栏,且不遮挡虚拟导航栏(兼容7.

          本文链接:https://www.haomeiwen.com/subject/fnoucxtx.html