关于StatusBar适配MD主题整理

主题使用:

  • 使用Theme.AppCompat.Light.NoActionBar(toolbar的兼容主题)

关于fitsSystemWindows 属性介绍:

  • 如果你希望拓展的区域不被状态栏遮挡住使用:fitsSystemWindows属性
  • fitsSystemWindows 按照深度优先的方式其作用,所以使用该属性的 View 的顺序是有要求的,如果第一个 View 使用了 inset (系统窗口的尺寸)则会导致其他 View 尺寸不一样。
  • Inset 总是相对于全屏幕的,Inset 可能在 View layout 之前就已经应用了,所以在设置 View 的 padding 之前 View 并不知道其具体相对于系统窗口的位置。
  • View 的其他 padding 值被重新改写了,在使用 fitsSystemWindows 为 true 的View 上设置 padding 值(paddingLeft/paddingTop/ 等)是没有效果的。
  • 比如 你想把 RecyclerView 的内容显示在一个透明导航栏的下面,就类似于 Google Now 一样,你可以在 RecyclerView 上设置 android:fitsSystemWindows=”true” ,然后在设置 RecyclerView 的 android:clipToPadding=”false”,这样这个 RecyclerView 就会显示在导航栏下方了,当你向上滑动 RecyclerView 到底的时候, RecyclerView 内容在导航栏上方,并没有被导航栏挡住。
  • 使用fitSystemWindows属性让系统帮我们自动适配不同情况下的status bar,让我们的view的paddingTop获取到一个合理的值。(还有其他的方案是通过手动设置paddingTop的值来进行适配的:在values-v19里设置paddingTop值为25dp,在values里设置为0dp,但是在某些自定义的rom里status bar的高度是被有修改过的。还有就是通过自定义继承toolbar,在代码里动态获取status bar的高度并设置paddingTop的值,但这样又弄得太麻烦了)。

code

BaseActivity
public class BaseActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //系统版本的校验
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        /**
         * 使用:FLAG_TRANSLUCENT_STATUS标志位,activity布局会扩展到状态栏
         * 
         */

        //设置参数,将status设置成透明
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//添加statusbar 透明
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);// 在存在虚拟按键的手机上,导航栏为透明
    }
  }
}
MainActivity
public class MainActivity extends BaseActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /**
     * 自定义toolbar的时候可以将下面注释掉,
     * 在系统布局中使用colorPrimary和minHeight指定颜色和高度
     */
    //将toolbar设置成actionbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(toolbar);

  }
}
activity_main.xml
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.example.leiiiooo.immersivedemo.MainActivity">
  <!--toolbar-->
  <!--<include
  layout="@layout/mytoolbar_layout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />-->
  <!--自定义topbar-->
  <include
    layout="@layout/mytopbar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />


  <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="ThinkCool" />

</LinearLayout>
mytoolbar_layout.xml
<android.support.v7.widget.Toolbar
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/my_toolbar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@color/colorPrimary"
  android:fitsSystemWindows="true"
  android:minHeight="?attr/actionBarSize"
  android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
mytopbar_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/colorPrimary"
          android:minHeight="?attr/actionBarSize"
          android:gravity="center"
          android:fitsSystemWindows="true"
          android:orientation="vertical">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:gravity="center"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/top_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/top_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="登陆"
        android:textSize="16sp" />

    <ImageView
        android:id="@+id/top_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />
  </LinearLayout>

</LinearLayout>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.leiiiooo.immersivedemo">
  <!--设置成没有actionbar的主题-->
  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".BaseActivity">
    </activity>
  </application>

</manifest>

效果如图:

使用系统toolbar
自定义toolbar+fitsSystemWindows
自定义toolbar 未设置fitsSystemWindows
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容