爆照
這年頭沒有好看的顏值都不好混,首先來一張全家福。
怎么樣添寺,一家六姊妹褪猛,總有一個你符合你的胃口的吧(什么赌结,六個都要吭露,小伙子不要太貪心胺痛椤)。
下面言歸正傳讲竿,分別以
沉浸式狀態(tài)欄三種實現(xiàn)方式
以及如何完美解決與鍵盤沖突
兩大方面說起
沉浸式狀態(tài)欄的三種實現(xiàn)方式
概述:大家都知道纬向,實現(xiàn)沉浸式狀態(tài)欄這種特性最低兼容到android4.4
,所以4.4之下的就不用去考慮了
方式一:設(shè)置ToolBar的Padding
Don't BB show me the code
mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
//透明狀態(tài)欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
mToolbar.setPadding(mToolbar.getPaddingLeft(),getStatusBarHeight(this),
mToolbar.getPaddingRight(),mToolbar.getPaddingBottom());
}
mToolbar.setTitle("測試一");
setSupportActionBar(mToolbar);
getStatusBarHeight(this)
獲取沉浸式狀態(tài)欄高度代碼
//通過反射獲取狀態(tài)欄高度,默認(rèn)25dp
private static int getStatusBarHeight(Context context) {
int statusBarHeight = dip2px(context, 25);
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height")
.get(object).toString());
statusBarHeight = context.getResources().getDimensionPixelSize(height);
} catch (Exception e) {
e.printStackTrace();
}
return statusBarHeight;
}
這樣就是就不需要我們在xml布局里面設(shè)置android:fitsSystemWindows="true"
屬性避免ToolBar
被狀態(tài)欄給遮蓋住
PS:當(dāng)然也可以不用ToolBar
通過include
一個布局替換戴卜,然后設(shè)置該布局的marginTop
就行,不過不建議這么做
方式二:設(shè)置屬性
android:fitsSystemWindows="true
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
//透明狀態(tài)欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:fitsSystemWindows="true"
android:id="@+id/toolbar"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"/>
</LinearLayout>
在ToolBar增加屬性android:fitsSystemWindows="true"
就行了
fitSystemWindows
屬性的官方描述:
Boolean internal attribute to adjust view layout based on system windows such as the status bar. If true, adjusts the padding of this view to leave space for the system windows. Will only take effect if this view is in a non-embedded activity.
方式三:使用SystemBarTint框架
在app
的build.gradle
添加依賴
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
主要設(shè)置代碼如下琢岩,具體可以參考SystemBarTint官方Demo
mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
// 創(chuàng)建狀態(tài)欄的管理實例
SystemBarTintManager tintManager = new SystemBarTintManager(this);
// 激活狀態(tài)欄設(shè)置
tintManager.setStatusBarTintEnabled(true);
// 激活導(dǎo)航欄設(shè)置投剥,當(dāng)使用actionbar的時候開啟
tintManager.setNavigationBarTintEnabled(true);
// 設(shè)置一個顏色給系統(tǒng)欄
tintManager.setTintColor(Color.parseColor("#990000FF"));
// 設(shè)置狀態(tài)欄需顏色或背景圖
tintManager.setStatusBarTintResource(R.color.colorAccent);
mToolbar.setTitle("測試三");
setSupportActionBar(mToolbar);
/**
* 是否設(shè)置沉浸式
* @param on
*/
@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);
}
完美解決與鍵盤沖突
最后講講我們在使用沉浸式結(jié)合輸入框遇到的坑
回顧一下之前開始我們的六朵金花
,最開始我們使用輸入框(當(dāng)然輸入框位置加上軟鍵盤的高度高于屏幕高度)會出現(xiàn)圖三的情況担孔,很直接想到的是忘記在Activity
設(shè)置軟鍵盤彈出模式,我們設(shè)置了
<activity android:name=".TestOneActivity"
android:windowSoftInputMode="adjustResize"/>
發(fā)現(xiàn)并沒有什么卵用(其實還是有卵用的江锨,至少不會出現(xiàn)標(biāo)題欄上移的情況.....雖然出現(xiàn)軟鍵盤遮蓋了輸入框)。
其實糕篇,這個問題gitHub
上早就有大牛有解決方案了啄育,他把他說成是google
的bug
,并給了相應(yīng)的解決方式AndroidBug5497Workaround
使用很簡單拌消,在Activity
onCreate
里的setContentView
方法之后調(diào)用AndroidBug5497Workaround2.assistActivity(this);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_one);
AndroidBug5497Workaround.assistActivity(this);
}
運行挑豌,出現(xiàn)了圖5
的結(jié)果,發(fā)現(xiàn)已經(jīng)幾乎符合我們的要求墩崩,可是仔細(xì)的看氓英,會看到軟鍵盤上面比圖6
多了一個狀態(tài)欄高度的白邊
這時候我們需要修改一下AndroidBug5497Workaround類里面的computeUsableHeight函數(shù)
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
private int computeUsableHeight() {
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
//這個判斷是為了解決19之后的版本在彈出軟鍵盤時,鍵盤和推上去的布局(adjustResize)之間有黑色區(qū)域的問題
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
return (r.bottom - r.top)+statusBarHeight;
}
return (r.bottom - r.top);
}
下面代碼就是在大牛的基礎(chǔ)上增加上狀態(tài)欄的高度鹦筹,至此铝阐,你成功達(dá)到了圖6
的效果。
插曲
為了實現(xiàn)ViewPager+Fragment+Menu(每個Fragment有各自的menu)铐拐,我把創(chuàng)建menu的方法搬到了每個Fragment里徘键,在Fragment的OnCreate設(shè)置setHasOptionsMenu(true);(中間涉及到ViewPager回調(diào)監(jiān)聽一些處理的細(xì)節(jié),這里不作詳細(xì)描述)好不容易在我的
華為榮耀6
實現(xiàn)了遍蟋,發(fā)現(xiàn)在華為榮耀8
不行吹害,后來google了好久知道了我看到這里 https://github.com/JakeWharton/ActionBarSherlock/issues/48,一個Jake Wharton大神提出的Android的一個bug匿值,里面給了解決的方法赠制,是需要繼承相應(yīng)封裝的Fragment和Activity,因為我已經(jīng)有基類了不太好修改,所以直接決定放棄了ToolBar+Menu 改成include布局钟些。(這一塊有時間我整理整理)
最后獻(xiàn)上gitHub鏈接android-transparent-status,歡迎star