ToolBar沉浸式状态栏的兼容实现

更新3:薄荷Toolbar(ActionBar)的适配方案

更新2:低版本实现Material Design 的两种方式
第三方库:com.readystatesoftware.systembartint
更新1 :发现了一个神奇方便的Utils for StatusBar 状态栏兼容类.
StatusBarCompat

values的dimens中

values中

<dimen name="toolBarPaddingTop">0dp</dimen>
<dimen name="toolBarHeight">50dp</dimen>

values-v19和values-v21中

<dimen name="toolBarPaddingTop">25dp</dimen>
<dimen name="toolBarHeight">75dp</dimen>

toolbar布局中

设置属性 android:fitsSystemWindows=”true”,此属性加在布局中不起作用,要加在布局中最靠近状态栏的第一个子控件中;
设置layout_height和paddingTop为我们设置好的
toolBarHeight,toolBarPaddingTop

<android.support.v7.widget.Toolbar
        android:id="@+id/base_toolbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolBarHeight"
        android:background="@android:color/holo_blue_light"
        android:fitsSystemWindows="true"
        android:paddingTop="@dimen/toolBarPaddingTop"
>
<!--android:theme="@style/Theme.Drawer.ArrowAnimation"-->

这里写图片描述

设置activity的属性

有两种方法,代码实现或者style文件实现

1.NoTitle、
2.在android 4.4以上即api19以上的状态栏为半透明
3.将我们自己的toolbar替换掉actionbar

代码实现:

在BaseActivity的onCreate中

//设置noTitle
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
//设置android 4.4以上即api19以上的状态栏为半透明
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams localLayoutParams = getWindow ().getAttributes ();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
//将我们自己的toolbar替换掉actionbar,最好让activity的theme是noActionbar的
setContentView(R.layout.activity_base_toolbar);
mToolbar = (Toolbar) findViewById(R.id.base_toolbar);
setSupportActionBar(mToolbar);

在style中设置

values中:

<style name="TranslucentTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
      //  <item name="colorPrimary">@color/colorPrimary</item>
      //  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
      //  <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="TranslucentTheme" parent="TranslucentTheme.Base">

values-v19中:

 <!-- Base application theme. -->
    <style name="TranslucentTheme" parent="TranslucentTheme.Base">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>

values-v21中:

<!-- Base application theme. -->
    <style name="TranslucentTheme" parent="TranslucentTheme.Base">
        <!-- Customize your theme here. -->
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
        <!--<item name="android:statusBarColor">@android:color/transparent</item>-->
    </style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值