生活随笔
收集整理的這篇文章主要介紹了
Android 沉浸式透明状态栏与导航栏
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Android 系統(tǒng)自4.2 開始 UI 上就沒多大改變,4.4 也只是增加了透明狀態(tài)欄與導航欄的功能
這個特性是andorid4.4支持的,最少要api19才可以使用。下面介紹一下使用的方法,非常得簡單:
安卓自定義狀態(tài)欄顏色以與APP風格保持一致
? ? 我們知道IOS上的應用,狀態(tài)欄的顏色總能與應用標題欄顏色保持一致,用戶體驗很不錯,那安卓是否可以呢?若是在安卓4.4之前,答案是否定的,但在4.4之后,谷歌允許開發(fā)者自定義狀態(tài)欄背景顏色啦,這是個不錯的體驗!若你手機上安裝有最新版的qq,并且你的安卓SDK版本是4.4及以上,你可以看下它的效果:
[java]?view plaincopy
public?class?MainActivity?extends?Activity?{?? ?? ????@Override?? ????protected?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????setContentView(R.layout.activity_main);?? ?? ?????????? ????????getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);?? ?????????? ????????getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);?? ?? ????}?? ?? ?? }??
[java]?view plaincopy
?? getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);?? ?? getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);??
只要加入這兩行代碼,就可以實現(xiàn)沉浸式通知欄了。效果如圖:
給大家看看這個界面的布局:
[html]?view plaincopy
<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:background="#ffffff"?? ????android:orientation="vertical"?? ????tools:context=".MainActivity">?? ?? ?? ????<TextView?? ?? ????????android:layout_width="match_parent"?? ????????android:layout_height="100dp"?? ????????android:background="#009959"?/>?? ?? ?? ????<Button?? ????????android:layout_width="100dp"?? ????????android:layout_height="50dp"?? ????????android:background="#ff669d"/>?? ?? </LinearLayout>??
是一個垂直的流布局,但這樣,其實還是有問題的,我在textView里面加一些文字,就是綠色的那一塊,大家看一下效果:
大家看到了吧,文字和狀態(tài)欄重疊在一起了,這肯定是不行的,此時需要添加下面的代碼:
[html]?view plaincopy
android:fitsSystemWindows="true"?? android:clipToPadding="true"??
[html]?view plaincopy
<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="#ffffff"?? ????android:orientation="vertical"?? ????tools:context=".MainActivity">?? ?? ?? ?? ????<TextView?? ????????android:layout_width="match_parent"?? ????????android:layout_height="100dp"?? ????????android:background="#009959"?/>?? ?? ?? ????<Button?? ????????android:layout_width="100dp"?? ????????android:layout_height="50dp"?? ????????android:background="#ff669d"/>?? ?? </LinearLayout>??
大家看紅色的那部分,加入那兩行以后,界面仍然會是沉浸式的,但狀態(tài)欄那部分,就不會再重疊了,像加了padding一樣,如下圖:
大家看圖,綠色的textView和紅色的一個button都被下移了,狀態(tài)欄是白色的,是背景l(fā)inearLayout的顏色。很明顯,這也不是我們想要的,我們希望狀態(tài)欄和我們放在頂部的控件是同一個顏色,同時,控件內(nèi)容也不和狀態(tài)欄重復,其實,只要把那兩行代碼放到我們頂部的控件就可以了。代碼如下:
[html]?view plaincopy
<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:background="#ffffff"?? ????android:orientation="vertical"?? ????tools:context=".MainActivity">?? ?? ?? ?? ????<TextView?? ????????android:fitsSystemWindows="true"?? ????????android:clipToPadding="true"?? ?? ????????android:layout_width="match_parent"?? ????????android:layout_height="100dp"?? ????????android:background="#009959"?? ????????android:text="你好,請問你有男朋友嗎"/>?? ?? ?? ????<Button?? ????????android:layout_width="100dp"?? ????????android:layout_height="50dp"?? ????????android:background="#ff669d"/>?? ?? </LinearLayout>??
就是那兩行紅色的代碼,放在綠色的textView上,這樣,就會是下面的效果:
這就是我們想要的了。
總結
以上是生活随笔為你收集整理的Android 沉浸式透明状态栏与导航栏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。