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与状态栏重叠

效果图:


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