实现类似Material Design 的状态栏

14 篇文章 0 订阅
2 篇文章 0 订阅

实现类似Material Design 的状态栏

@(Blog)[马克飞象|Markdown|Android]

Android 4.4自带方法

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    //透明状态栏
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    //透明导航栏
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>
<item name="android:windowTranslucentNavigation" tools:targetApi="kitkat">true</item>

上面的代码和style实现的同一个功能,只需要使用一种即可。
如果只是使用了以上的代码,只是实现了一个全屏效果,我们还需要在布局文件中加入两个属性
Alt text

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:fitsSystemWindows="true"
    android:clipToPadding="true"
    android:background="@color/status_bar_blue"

    tools:context="com.example.focus.androidnote.statusbar.StatusBarActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/toolbar_blue"/>
</LinearLayout>

Text
在网上找了很久,都没有发现可以单独设置StatusBar颜色的方法,所以只能同通过设置布局文件的Root背景色来实现StatusBar的颜色设置

使用SystemBarTint

由于使用系统自带API只能通过设置Background来实现为StatusBar赋颜色,我们可以使用开源库SystemBarTint,来解决这个问题

  • 代码
/* 激活主题 */
// 创建状态栏的管理实例
SystemBarTintManager tintManager = new SystemBarTintManager(this);
// 激活状态栏设置
tintManager.setStatusBarTintEnabled(true);
// 激活导航栏设置
tintManager.setNavigationBarTintEnabled(true);

/* 设置主题 */
// 设置一个状态栏资源
tintManager.setStatusBarTintColor(R.color.status_bar_blue);
  • 布局
    布局上和使用系统方法时一样,我们依然需要设置android:fitsSystemWindowsandroid:clipToPadding,但是要注意android:clipToPadding不再是在Root上,而是要设置到Toolbar
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:clipToPadding="true"

    android:background="@color/background_blue"
    tools:context="com.example.focus.androidnote.statusbar.StatusBarActivity">

    <android.support.v7.widget.Toolbar
        android:fitsSystemWindows="true"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/toolbar_blue"/>

</LinearLayout>

效果图

参考
http://blog.csdn.net/spring_he/article/details/39701419
http://stackoverflow.com/questions/20781014/translucent-system-bars-and-content-margin-in-kitkat

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值