Android4.4+ 實現(xiàn)半透明狀態(tài)欄(Translucent Bars)
Android從4.4(KitKat) 開始進行了一些視覺上的改善和提升,其中包括讓狀態(tài)欄(Status Bar)和下方導航欄(Navigation Bar)進行半透明處理,可以使APP內(nèi)容向上下延伸,使整個畫面的利用度大幅度提升,本篇就來說說這個“半透明狀態(tài)欄”(Translucent Bars)。
簡單做了個Demo效果如下圖

*這里解釋個誤區(qū),國內(nèi)開發(fā)者和設計師經(jīng)常把這種半透明效果稱為沉浸式狀態(tài)欄這是不對的, 沉浸式Immersive mode,官方解釋為hiding all system UI根本不是這種半透明的效果。
下面說說如何使用這種效果:
1、在onCreate里面代碼設置半透明的屬性,由于只有Android 4.4以上才支持這種效果,所以代碼需要判斷下
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//透明狀態(tài)欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明底部導航欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
2、在這個界面上我去掉了Actionbar,實現(xiàn)方式有很多,這里我使用的是在Style里去掉。
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
3、這個部分需要留意一下,如果希望APP的顯示內(nèi)容正常和滾動透明化需要加上android:fitsSystemWindows=”true”和android:clipToPadding=”false”的屬性,建議你把這兩個屬性好好試試加上與否的區(qū)別。
<ScrollView 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="false"
android:background="#795548"
tools:context=".DefaultActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#ffffff"
android:text="@string/str" />
</ScrollView>
這樣一個簡單的半透明化效果就實現(xiàn)了
詳細源碼:
Layout
<ScrollView 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="false"
android:background="#795548"
tools:context=".DefaultActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#ffffff"
android:text="@string/str" />
</ScrollView>
Style
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//透明狀態(tài)欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明導航欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
setContentView(R.layout.activity_main);
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android自定義DataTimePicker實例代碼(日期選擇器)
本篇文章主要介紹了Android自定義DataTimePicker實例代碼(日期選擇器),非常具有實用價值,需要的朋友可以參考下。2017-01-01
詳解Android中Intent對象與Intent Filter過濾匹配過程
這篇文章主要介紹了Android中Intent對象與Intent Filter過濾匹配過程,感興趣的小伙伴們可以參考一下2015-12-12
Android 解決ScrollView嵌套CridView顯示問題
這篇文章主要介紹了Android 解決ScrollView嵌套CridView顯示問題的相關資料,使用ScrollView嵌套CridView的時候會出現(xiàn)顯示不全的問題,這里提供解決辦法,需要的朋友可以參考下2017-08-08
Android中okhttp3.4.1+retrofit2.1.0實現(xiàn)離線緩存
這篇文章主要介紹了Android中okhttp3.4.1結合retrofit2.1.0實現(xiàn)離線緩存,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
android項目從Eclipse遷移到Android studio中常見問題解決方法
android項目從Eclipse遷移到Android studio中經(jīng)常會遇到一些問題,本文提供了Android studio使用中常見問題解決方法2018-03-03

