另外兩種Android沉浸式狀態(tài)欄實(shí)現(xiàn)思路
關(guān)于沉浸式狀態(tài)欄相信大家都不陌生,IOS系統(tǒng)很早就有,android5.0及以后版本都支持給狀態(tài)欄著色,而目前android主流版本還是4.4,網(wǎng)上通用實(shí)現(xiàn)4.4(API19)沉浸式狀態(tài)欄也都是依賴于可以將狀態(tài)欄變?yōu)橥该鞯膶傩?,再為其著色,主要?shí)現(xiàn)代碼:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match_actionbar);
//只對api19以上版本有效
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
//為狀態(tài)欄著色
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.statusbar_bg);
}
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
再在根布局添加以下兩個屬性:
android:fitsSystemWindows="true" android:clipToPadding="false"
這樣就可以了,以上著色使用了SystemBarTint。
為什么我要尋找其他的方案?
面對大多數(shù)的界面自然是沒有多大問題,但是針對類似QQ這種側(cè)滑的界面,如圖:

我的手機(jī)系統(tǒng)版本是4.4的,如果想做成QQ側(cè)滑背景這樣的效果,使用上面的方案就變成了這樣

這樣出來的效果就會很丑,于是才有了改進(jìn)版的方案,不知QQ是否是這樣做的。
除了上述的缺陷以外,還有一點(diǎn)看著不是很舒服,就是當(dāng)我使用抽屜菜單或者滑動返回效果的時候是這樣的

狀態(tài)欄并沒有陰影效果
我想要的效果是這樣的

狀態(tài)欄也會跟著一起滑動
第一種思路
自定義一個狀態(tài)欄,不能添加“ android:fitsSystemWindows="true"
”這個屬性,不然無法填充到狀態(tài)欄,如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:orientation="vertical">
<View
android:id="@+id/status_bar"
android:layout_width="match_parent"
android:layout_height="20dp"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
在到代碼中判斷
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View statusBar = findViewById(R.id.status_bar);
setContentView(R.layout.activity_test);
//判斷SDK版本是否大于等于19,大于就讓他顯示,小于就要隱藏,不然低版本會多出來一個
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
statusBar.setVisibility(View.VISIBLE);
//還有設(shè)置View的高度,因?yàn)槊總€型號的手機(jī)狀態(tài)欄高度都不相同
}else{
statusBar.setVisibility(View.GONE);
}
}
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
其實(shí),這樣已經(jīng)解決了我上面提出的兩個問題
第二種實(shí)現(xiàn)方案
第二種方案是為了解決第一種方案中遇到的奇葩問題,設(shè)置了透明屬性的界面(聊天及底下評論的框框)不能被系統(tǒng)輸入法頂上去,之前寫過一篇Android 聊天界面背景圖片被輸入法“頂上去”問題解析,現(xiàn)在遇到的就是無論如何聊天的輸入框都不能被系統(tǒng)輸入法頂上去(就是打字看不到輸入框),經(jīng)過一番測試,發(fā)現(xiàn)竟然和“ android:fitsSystemWindows="true"
”這個屬性有關(guān),加上去輸入框就沒問題,但自定義的狀態(tài)欄不能被填充到真正的狀態(tài)欄位置

陷入了兩難的境地,加還是不加都有問題,而且都特別明顯,說了半天,來看看第二種方案。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="@android:color/holo_green_light"
android:fitsSystemWindows="true"
tools:context="com.saidtx.myapplication.TestActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/edit"
android:background="@android:color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:text="@string/previews"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/white">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
關(guān)鍵在于下面兩個屬性,還有需要在其他子布局添加背景,不然就跟隨了最外層的背景,代碼部分還是采用網(wǎng)上通用方案,只是不需要自定義的狀態(tài)欄了,也不需要計算狀態(tài)欄的高度
android:fitsSystemWindows="true" android:background="@android:color/holo_green_light"
最終效果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)Android軟件編程有所幫助。
- Android 實(shí)現(xiàn)沉浸式狀態(tài)欄的方法
- Android 沉浸式狀態(tài)欄與隱藏導(dǎo)航欄實(shí)例詳解
- 解決Android 沉浸式狀態(tài)欄和華為虛擬按鍵沖突問題
- Android之沉浸式狀態(tài)欄的實(shí)現(xiàn)方法、狀態(tài)欄透明
- Android沉浸式狀態(tài)欄微技巧(帶你真正理解沉浸式模式)
- Android 4.4以上"沉浸式"狀態(tài)欄效果的實(shí)現(xiàn)方法
- Android App仿QQ制作Material Design風(fēng)格沉浸式狀態(tài)欄
- Android編程中沉浸式狀態(tài)欄的三種實(shí)現(xiàn)方式詳解
- Android 高仿QQ 沉浸式狀態(tài)欄
- Android實(shí)現(xiàn)沉浸式狀態(tài)欄功能
相關(guān)文章
Android ViewPager相冊橫向移動的實(shí)現(xiàn)方法
本篇文章小編為大家介紹,Android ViewPager相冊橫向移動的實(shí)現(xiàn)方法。需要的朋友參考下2013-04-04
Android仿新浪微博發(fā)送菜單界面的實(shí)現(xiàn)
這篇文章主要介紹了Android仿新浪微博發(fā)送菜單界面的實(shí)現(xiàn),幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-04-04
android短信管理器SmsManager實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了android短信管理器SmsManager實(shí)例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
Android編程之自定義AlertDialog(退出提示框)用法實(shí)例
這篇文章主要介紹了Android編程之自定義AlertDialog(退出提示框)用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了自定義AlertDialog的頁面布局與功能實(shí)現(xiàn)相關(guān)技巧,需要的朋友可以參考下2016-01-01
android中SwipeRefresh實(shí)現(xiàn)各種上拉,下拉刷新示例
這篇文章主要介紹了android中SwipeRefresh實(shí)現(xiàn)各種上拉,下拉刷新示例,非常具有實(shí)用價值,需要的朋友可以參考下。2017-03-03
Android Animation實(shí)戰(zhàn)之屏幕底部彈出PopupWindow
這篇文章主要為大家介紹了Android Animation動畫實(shí)戰(zhàn)項(xiàng)目,屏幕底部彈出PopupWindow,如何實(shí)現(xiàn)?文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01
Android onActivityResult和setResult方法詳解及使用
這篇文章主要介紹了Android onActivityResult和setResult方法詳解及使用的相關(guān)資料,這里提供實(shí)例,幫助大家學(xué)習(xí)理解,需要的朋友可以參考下2016-12-12
Android中@id和@+id及@android:id的區(qū)別介紹
這篇文章主要給大家介紹了關(guān)于Android中@id和@+id及@android:id的區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Android編程實(shí)現(xiàn)webview執(zhí)行l(wèi)oadUrl時隱藏鍵盤的workround效果
這篇文章主要介紹了Android編程實(shí)現(xiàn)webview執(zhí)行l(wèi)oadUrl時隱藏鍵盤的workround效果,較為詳細(xì)的分析了執(zhí)行l(wèi)oadUrl時隱藏鍵盤的workround具體步驟與兩種實(shí)現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10

