Android 漸變色沉浸式狀態列
阿新 • • 發佈:2019-01-08
效果圖在最下
在AndroidManifest.xml中的activity中使用自定義主題
<activity android:name=".dailyAccompany.view.activity.AlarmListActivity"
android:theme="@style/MyStatusBarTheme"
android:screenOrientation="portrait">
</activity>
然後在style.xml中去定義該主題
<style name="MyStatusBarTheme" parent="Theme.AppCompat.DayNight.NoActionBar"> <item name="android:windowTranslucentNavigation">true</item> <item name="android:windowTranslucentStatus">true</item> <!-- 設定statusBarColor 為透明--> <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> 就可以實現了