概述
在一個人云亦云的時代,在技術迅猛發展的時代,我們來不及去好好學習,好好理解,就這樣和著別人說話,都沒有好好思考過。由此引發了這一血案。
是透明不是沉浸
雖然說不大懂英文也是可以擼代碼的,但是我總覺得此非長久之計也。英文還是得多學,免得阻礙了發展道路。由于對英文不感冒,所以才有了將Translucent Bar(透明欄)和Immersive Mode(沉浸模式)混為一談的說法。下面是引自Android開發者官網的一段說法
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(狀態欄)使屏幕全屏,讓Activity接收所有的(整個屏幕的)觸摸事件。 - 透明化系統欄
透明化系統欄,使得布局侵入系統欄的后面,必須啟用fitsSystemWindows屬性來調整布局才不至于被系統欄覆蓋。
如何在4.4以上中實現Translucent Bar
為什么要強調4.4呢?其一是Translucent Bar是4.4開始有的特性;其二是5.0開始,Google推出Material Design,使用Theme.Material(MD主題)或Theme.AppCompat主題并至少設置ColorPrimaryDark屬性就可以實現status bar半透明效果。
跟著官方走
其實在上面,官方的說法已經很清楚了
第一步. 使用主題:
Theme.Holo.NoActionBar.TranslucentDecorandTheme.Holo.Light.NoActionBar.TranslucentDecor
點擊上面的主題鏈接看一下,原來只是設置了屬性 windowTranslucentStatus 和 windowTranslucentNavigation 為true,我們可以不用官方說的主題,但是必須設置那兩個屬性的值為true才行,以便使我們定義的layout侵入系統欄的領域。說到這里,我們有必要聊聊什么事系統欄(system bar)。看下面的圖,系統欄包含了頂部狀態欄和底部導航欄。
第二步. 設置 fitsSystemWindows 屬性為true來進行布局調整
官方描述:
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.
簡單翻譯下,就是哪個view設置了這個屬性為true,系統就會調整該view的padding值來留出空間給系統窗體。表現為,padding出status bar的高度給status bar使用,不至于我們定義layout跟status bar重疊!
實際的使用可以參考Clock哥的簡書:Translucent System Bar 的最佳實踐,在此就不贅述了。
值的一提的是:該屬性最好是用在我們需要進行調整的view中,而不是在theme中設置,因為theme設置的是整個窗體的屬性,會影響使用了該theme的activity或application的行為,造成依附于Application Window的Window(比如Toast)錯位。還有當Activity的ContentView中嵌套的layout有好幾處使用了該屬性時,會發生奇怪的錯位問題。記住:只在需要padding出空間給system bar的layout添加該屬性!
Toast錯位的解決方法如下:使用應用級別的上下文
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>
第二步. 在基類中完成布局調整的工作
//摘自: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());//把設置布局文件的操作交給繼承的子類
ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
View parentView = contentFrameLayout.getChildAt(0);
if (parentView != null && Build.VERSION.SDK_INT >= 14) {
parentView.setFitsSystemWindows(true);
}
}
/**
* 返回當前Activity布局文件的id
*
* @return
*/
abstract protected int getLayoutResId();
}
簡述下他的思路:試圖將設置內容布局的操作交給子類,而基類獲取內容布局的根布局,判斷是否為4.4或以上的運行環境,再設置fitsSystemWindows屬性為true,達到調整內容布局的目的。
自己動手,豐衣足食
- 啟用透明化:同上,不贅述
- 自己fitsSystemWindows,不用系統幫我
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());
//顏色的設置可抽取出來讓子類實現之
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;
}
}
效果圖如下,會有一條莫名的黑線,暫時沒有找到解決方案,知道的童鞋不妨交流一下。
簡述下我的思路:
Activity持有一個PhoneWindow,PhoneWindow持有一個根View,叫DecorView(是一個FrameLayout),DecorView持有一個LinearLayout,在LinearLayout下分配兩個FrameLayout,一個給ActionBar(當設置主題為NoActionBar是為ViewStub),一個給ContentView。不管如何,只要我們在LinearLayout的第一個位置插入一個View就可以讓ContentView下移了!!
更新日志 ↓ ↓ ↓
3月3日更新:
- 優化文檔結構,方便閱讀