概述
在一個(gè)人云亦云的時(shí)代,在技術(shù)迅猛發(fā)展的時(shí)代,我們來不及去好好學(xué)習(xí),好好理解,就這樣和著別人說話,都沒有好好思考過。由此引發(fā)了這一血案。
是透明不是沉浸
雖然說不大懂英文也是可以擼代碼的,但是我總覺得此非長久之計(jì)也。英文還是得多學(xué),免得阻礙了發(fā)展道路。由于對英文不感冒,所以才有了將Translucent Bar(透明欄)和Immersive Mode(沉浸模式)混為一談的說法。下面是引自Android開發(fā)者官網(wǎng)的一段說法
Immersive full-screen mode
To provide your app with a layout that fills the entire screen, the new SYSTEM_UI_FLAG_IMMERSIVE flag for setSystemUiVisibility()(when combined with SYSTEM_UI_FLAG_HIDE_NAVIGATION enables a new immersivefull-screen mode. While immersive full-screen mode is enabled, your activity continues to receive all touch events. The user can reveal the system bars with an inward swipe along the region where the system bars normally appear. This clears the SYSTEM_UI_FLAG_HIDE_NAVIGATION flag (and the SYSTEM_UI_FLAG_FULLSCREEN flag, if applied) so the system bars remain visible. However, if you'd like the system bars to hide again after a few moments, you can instead use the SYSTEM_UI_FLAG_IMMERSIVE_STICKY flag.
Translucent system bars
You can now make the system bars partially translucent with new themes, Theme.Holo.NoActionBar.TranslucentDecor and Theme.Holo.Light.NoActionBar.TranslucentDecor.By enabling translucent system bars, your layout will fill the area behind the system bars, so you must also enable fitsSystemWindows for the portion of your layout that should not be covered by the system bars.
If you're creating a custom theme, set one of these themes as the parent theme or include the windowTranslucentNavigation and windowTranslucentStatus style properties in your theme.
簡單翻譯一下:
- 沉浸式全屏模式
隱藏status bar(狀態(tài)欄)使屏幕全屏,讓Activity接收所有的(整個(gè)屏幕的)觸摸事件。 - 透明化系統(tǒng)欄
透明化系統(tǒng)欄,使得布局侵入系統(tǒng)欄的后面,必須啟用fitsSystemWindows屬性來調(diào)整布局才不至于被系統(tǒng)欄覆蓋。
如何在4.4以上中實(shí)現(xiàn)Translucent Bar
為什么要強(qiáng)調(diào)4.4呢?其一是Translucent Bar是4.4開始有的特性;其二是5.0開始,Google推出Material Design,使用Theme.Material(MD主題)或Theme.AppCompat主題并至少設(shè)置ColorPrimaryDark屬性就可以實(shí)現(xiàn)status bar半透明效果。
跟著官方走
其實(shí)在上面,官方的說法已經(jīng)很清楚了
第一步. 使用主題:
Theme.Holo.NoActionBar.TranslucentDecorandTheme.Holo.Light.NoActionBar.TranslucentDecor
點(diǎn)擊上面的主題鏈接看一下,原來只是設(shè)置了屬性 windowTranslucentStatus 和 windowTranslucentNavigation 為true,我們可以不用官方說的主題,但是必須設(shè)置那兩個(gè)屬性的值為true才行,以便使我們定義的layout侵入系統(tǒng)欄的領(lǐng)域。說到這里,我們有必要聊聊什么事系統(tǒng)欄(system bar)。看下面的圖,系統(tǒng)欄包含了頂部狀態(tài)欄和底部導(dǎo)航欄。
第二步. 設(shè)置 fitsSystemWindows 屬性為true來進(jìn)行布局調(diào)整
官方描述:
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.
簡單翻譯下,就是哪個(gè)view設(shè)置了這個(gè)屬性為true,系統(tǒng)就會調(diào)整該view的padding值來留出空間給系統(tǒng)窗體。表現(xiàn)為,padding出status bar的高度給status bar使用,不至于我們定義layout跟status bar重疊!
實(shí)際的使用可以參考Clock哥的簡書:Translucent System Bar 的最佳實(shí)踐,在此就不贅述了。
值的一提的是:該屬性最好是用在我們需要進(jìn)行調(diào)整的view中,而不是在theme中設(shè)置,因?yàn)閠heme設(shè)置的是整個(gè)窗體的屬性,會影響使用了該theme的activity或application的行為,造成依附于Application Window的Window(比如Toast)錯位。還有當(dāng)Activity的ContentView中嵌套的layout有好幾處使用了該屬性時(shí),會發(fā)生奇怪的錯位問題。記住:只在需要padding出空間給system bar的layout添加該屬性!
Toast錯位的解決方法如下:使用應(yīng)用級別的上下文
Toast.makeText(getApplicationContext(),"toast sth...",Toast.LENGTH_SHORT).show();
跟著民間大神走
第一步. 在res/values/文件夾中添加style-v19.xml,開啟透明欄
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
</resources>
第二步. 在基類中完成布局調(diào)整的工作
//摘自:http://www.lxweimin.com/p/0acc12c29c1b
public abstract class TranslucentBarBaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(getLayoutResId());//把設(shè)置布局文件的操作交給繼承的子類
ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
View parentView = contentFrameLayout.getChildAt(0);
if (parentView != null && Build.VERSION.SDK_INT >= 14) {
parentView.setFitsSystemWindows(true);
}
}
/**
* 返回當(dāng)前Activity布局文件的id
*
* @return
*/
abstract protected int getLayoutResId();
}
簡述下他的思路:試圖將設(shè)置內(nèi)容布局的操作交給子類,而基類獲取內(nèi)容布局的根布局,判斷是否為4.4或以上的運(yùn)行環(huán)境,再設(shè)置fitsSystemWindows屬性為true,達(dá)到調(diào)整內(nèi)容布局的目的。
自己動手,豐衣足食
- 啟用透明化:同上,不贅述
- 自己fitsSystemWindows,不用系統(tǒng)幫我
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
ViewGroup firstChildAtDecorView = ((ViewGroup) ((ViewGroup)getWindow().getDecorView()).getChildAt(0));
View statusView = new View(this);
ViewGroup.LayoutParams statusViewLp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
getStatusBarHeight());
//顏色的設(shè)置可抽取出來讓子類實(shí)現(xiàn)之
statusView.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
firstChildAtDecorView.addView(statusView, 0, statusViewLp);
}
}
private int getStatusBarHeight() {
int resId = getResources().getIdentifier("status_bar_height","dimen","android");
if(resId>0){
return getResources().getDimensionPixelSize(resId);
}
return 0;
}
}
效果圖如下,會有一條莫名的黑線,暫時(shí)沒有找到解決方案,知道的童鞋不妨交流一下。
簡述下我的思路:
Activity持有一個(gè)PhoneWindow,PhoneWindow持有一個(gè)根View,叫DecorView(是一個(gè)FrameLayout),DecorView持有一個(gè)LinearLayout,在LinearLayout下分配兩個(gè)FrameLayout,一個(gè)給ActionBar(當(dāng)設(shè)置主題為NoActionBar是為ViewStub),一個(gè)給ContentView。不管如何,只要我們在LinearLayout的第一個(gè)位置插入一個(gè)View就可以讓ContentView下移了!!
更新日志 ↓ ↓ ↓
3月3日更新:
- 優(yōu)化文檔結(jié)構(gòu),方便閱讀