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

*這里解釋個(gè)誤區(qū),國內(nèi)開發(fā)者和設(shè)計(jì)師經(jīng)常把這種半透明效果稱為沉浸式狀態(tài)欄這是不對(duì)的, 沉浸式Immersive mode,官方解釋為hiding all system UI根本不是這種半透明的效果。
下面說說如何使用這種效果:
1、在onCreate里面代碼設(shè)置半透明的屬性,由于只有Android 4.4以上才支持這種效果,所以代碼需要判斷下
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//透明狀態(tài)欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明底部導(dǎo)航欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
2、在這個(gè)界面上我去掉了Actionbar,實(shí)現(xiàn)方式有很多,這里我使用的是在Style里去掉。
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
3、這個(gè)部分需要留意一下,如果希望APP的顯示內(nèi)容正常和滾動(dòng)透明化需要加上android:fitsSystemWindows=”true”和android:clipToPadding=”false”的屬性,建議你把這兩個(gè)屬性好好試試加上與否的區(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>
這樣一個(gè)簡單的半透明化效果就實(shí)現(xiàn)了
詳細(xì)源碼:
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);
//透明導(dǎo)航欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
setContentView(R.layout.activity_main);
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android仿Iphone屏幕底部彈出半透明PopupWindow效果
- Android實(shí)現(xiàn)底部半透明彈出框PopUpWindow效果
- Android中設(shè)置組件半透明和透明的效果示例
- Android編程自定義圓角半透明Dialog的方法
- Android開發(fā)中Dialog半透明背景消失
- Android Menu半透明效果的開發(fā)實(shí)例
- Android實(shí)現(xiàn)在列表List中顯示半透明小窗體效果的控件用法詳解
- Android編程實(shí)現(xiàn)popupwindow彈出后屏幕背景變成半透明效果
- Android編程實(shí)現(xiàn)設(shè)置按鈕背景透明與半透明及圖片背景透明的方法
相關(guān)文章
Android Studio一直處于Building的兩種解決方法
很多朋友都遇到過打開別人的項(xiàng)目一直處于Building‘XXX’Gradle project info的情況。下面小編給大家?guī)砹薃ndroid Studio一直處于Building的解決方法,感興趣的朋友一起看看吧2018-08-08
Android實(shí)現(xiàn)EditText中添加和刪除bitmap的方法
這篇文章主要介紹了Android實(shí)現(xiàn)EditText中添加和刪除bitmap的方法,實(shí)例分析了Android中EditText控件的bitmap操作技巧,需要的朋友可以參考下2016-01-01
仿網(wǎng)易新聞客戶端頭條ViewPager嵌套實(shí)例
正確使用requestDisallowInterceptTouchEvent(boolean flag)方法,下面為大家介紹下外層ViewPager布局的實(shí)例,感興趣的朋友可以參考下哈2013-06-06
Flutter學(xué)習(xí)LogUtil封裝與實(shí)現(xiàn)實(shí)例詳解
這篇文章主要為大家介紹了Flutter學(xué)習(xí)LogUtil封裝與實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android開發(fā)簽名知識(shí)梳理總結(jié)
這篇文章主要介紹了Android開發(fā)簽名知識(shí)梳理總結(jié),Android?系統(tǒng)要求所有?APK?必須先使用證書進(jìn)行數(shù)字簽名,然后才能安裝到設(shè)備上進(jìn)行更新2022-06-06
RecyclerView實(shí)現(xiàn)探探卡片滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)探探卡片滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
淺談Android IPC機(jī)制之Binder的工作機(jī)制
IPC機(jī)制即為跨進(jìn)程通信,是inter-Process Communication的縮寫。是指兩個(gè)進(jìn)程之間進(jìn)行通信。在說進(jìn)程通信之前,我們的弄明白什么是線程,什么是進(jìn)程。進(jìn)程和線程是兩個(gè)截然不同的概念。本文將介紹Android IPC機(jī)制之Binder的工作機(jī)制。2021-06-06
Android 仿微信自定義數(shù)字鍵盤的實(shí)現(xiàn)代碼
本篇文章主要介紹了Android 仿微信自定義數(shù)字鍵盤的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07

