Android 沉浸式状态栏攻略 让你的状态栏变色吧

本文详细介绍了如何在Android应用中实现半透明状态栏,包括colors.xml和styles.xml的配置,布局文件的设定,以及在Activity中处理4.4和5.x显示方案的差异。通过实例代码展示了如何使状态栏与Toolbar融合,提供了一致的视觉体验。
摘要由CSDN通过智能技术生成
  • 4.4 模拟器

  • 5.x 真机

[new]贴个如果顶部是图片的效果图,其实是一样的,为了方便我就放侧栏的顶部了。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

稍等,csdn图片服务器异常…

ok,有了效果图之后就开始看实现了。


三、实现半透明状态栏

因为本例使用了NavigationView,所以布局代码稍多,当然如果你不需要,可以自己进行筛减。

注意引入相关依赖:

compile ‘com.android.support:appcompat-v7:22.2.1’

compile ‘com.android.support:support-v4:22.2.1’

compile ‘com.android.support:design:22.2.0’

(一)colors.xml 和 styles.xml

首先我们定义几个颜色:

res/values/color.xml

<?xml version="1.0" encoding="utf-8"?>

#FF03A9F4

#FF0288D1

@color/primary_dark

下面定义几个styles.xml

注意文件夹的路径:

values/styles.xml

values-v19

ok,这个没撒说的。注意我们的主题是基于NoActionBar的,android:windowTranslucentStatus这个属性是v19开始引入的。

(二)布局文件

activity_main.xml

<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:layout_width=“match_parent”

android:layout_height=“match_parent”

<LinearLayout

android:id=“@+id/id_main_content”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”>

<android.support.v7.widget.Toolbar

android:id=“@+id/id_toolbar”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:background=“?attr/colorPrimary”

android:fitsSystemWindows=“true”

app:popupTh

  • 27
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值