4.4适配沉浸式详解

来源:互联网 发布:php implode 什么意思 编辑:程序博客网 时间:2024/06/16 01:01

4.4适配沉浸式

values/colors.xml

<?xml version="1.0" encoding="utf-8"?><resources>        <color name="colorPrimary">#3F51B5</color>        <color name="colorPrimaryDark">#303F9F</color>        <color name="colorAccent">#FF4081</color></resources>

values/styles.xml

 <!-- Base application theme. -->    <style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>        <item name="colorAccent">@color/colorAccent</item>    </style>    <style name="AppTheme" parent="@style/BaseTheme">    </style>

values-v19/styles.xml


<resources>    <style name="AppTheme" parent="@style/BaseTheme">        <item name="android:windowTranslucentStatus">true</item>    </style></resources>

values-v21/styles.xml

<resources>    <style name="AppTheme" parent="@style/BaseTheme">        <item name="android:windowDrawsSystemBarBackgrounds">true</item>        <item name="android:statusBarColor">@android:color/transparent</item>    </style></resources>

布局文件


layout/app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true"    tools:context="com.example.fullstack.MainActivity">    <android.support.design.widget.AppBarLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:theme="@style/AppTheme.AppBarOverlay">        <android.support.v7.widget.Toolbar            android:id="@+id/toolbar"            android:layout_width="match_parent"            android:layout_height="?attr/actionBarSize"            android:background="?attr/colorPrimary"            app:popupTheme="@style/AppTheme.PopupOverlay" />    </android.support.design.widget.AppBarLayout>    <include layout="@layout/content_main" />    <android.support.design.widget.FloatingActionButton        android:id="@+id/fab"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="bottom|end"        android:layout_margin="@dimen/fab_margin"        android:src="@android:drawable/ic_dialog_email" /></android.support.design.widget.CoordinatorLayout>

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/drawer_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:openDrawer="start">    <include        layout="@layout/app_bar_main"        android:layout_width="match_parent"        android:layout_height="match_parent" />    <android.support.design.widget.NavigationView        android:id="@+id/nav_view"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_gravity="start"        app:headerLayout="@layout/nav_header_main"        app:menu="@menu/activity_main_drawer" /></android.support.v4.widget.DrawerLayout>

*注意点:


  • NavigationView不设置android:fitsSystemWindows=”true”
  • CoordinatorLayout设置android:fitsSystemWindows=”true”
  • values-v21下设置Theme属性android:statusBarColor为透明

Activity代码

使用SystemBarTintManager(https://github.com/jgilfelt/SystemBarTint),设置状态栏颜色

注:
5.0以上Android版本会自动适配,theme主题中的属性,不需要额外适配。

if(Build.VERSION.SDK_INT==Build.VERSION_CODES.KITKAT){            SystemBarTintManager systemBarTintManager=new SystemBarTintManager(this);            systemBarTintManager.setStatusBarTintEnabled(true);         systemBarTintManager.setStatusBarTintResource(statusBarColor);            systemBarTintManager.setNavigationBarAlpha(0.2f);        }

如果当前Android版本等于4.4,使用SystemBarTintManager适配。

0 0