Android 渐变色沉浸式状态栏

效果图在最下

在AndroidManifest.xml中的activity中使用自定义主题
<activity android:name=".dailyAccompany.view.activity.AlarmListActivity"
    android:theme="@style/TranslucentStatusTheme"
    android:screenOrientation="portrait">
</activity>

然后在style.xml中去定义该主题

要在三个不同的styles.xml文件中定义以适应不同安卓版本


values的styles.xml

<!--透明状态栏-->
<style name="TranslucentStatusTheme" parent="Theme.AppCompat.Light.NoActionBar">

</style>

values-v19的styles.xml

<style name="TranslucentStatusTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>

values-v21的styles.xml

<style name="TranslucentStatusTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

在布局文件中的第一个控件设置为渐变

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/alarm_shade_bg"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <ImageView
        android:id="@+id/alarm_top"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:background="@drawable/title_shade_bule"/>

       省略中间布局。。。。。。。。。。。。。。。。。。

</android.support.constraint.ConstraintLayout>

然后去定义渐变布局title_shade_bule

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="180"
        android:centerColor="#2798f0"
        android:endColor="#6043ce"
        android:startColor="#6043ce"
        android:type="linear" />
</shape>
就可以实现了

注:如果顶部是使用Toolbar,需要在Toolbar中加上

android:fitsSystemWindows="true"

以防止Toolbar与状态栏重叠

效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

break妖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值