Android Studio 全屏沉浸式透明狀態列效果的實現
阿新 • • 發佈:2020-11-30
如何實現?1.)首先實現全屏
第一種:繼承主題特定主題
在Android API 19以上可以使用****.TranslucentDecor***有關的主題,自帶相應半透明效果,Theme.Holo.NoActionBar.TranslucentDecor和Theme.Holo.Light.NoActionBar.TranslucentDecor兩種主題為新增加的,所以要新建values-v19資料夾並建立styles檔案新增如下程式碼
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.NoActionBar.TranslucentDecor"> <!-- Customize your theme here. --> </style>
第二種:在activity中採用程式碼的方式
Android 4.4以上可以新增如下程式碼
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態列 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導航欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); }
Android 5.0 以上也可以使用下面的程式碼實現全屏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); }
2.)解決狀態列佔位問題
第一種:主題新增如下設定
<item name="android:fitsSystemWindows">true</item>
第二種:activity layout根目錄新增下面程式碼
android:fitsSystemWindows="true"
第三種:通過Java程式碼設定
rootview.setFitsSystemWindows(true);
3.)狀態列導航欄設定背景色
4.4以上的可以採用修改contentView的背景色,或者動態新增一個view到contentView上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態列 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導航欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); //設定contentview為fitsSystemWindows ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content); View childAt = contentView.getChildAt(0); if (childAt != null) { childAt.setFitsSystemWindows(true); } //給statusbar著色 View view = new View(this); view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight(this))); view.setBackgroundColor(color); contentView.addView(view); }
動態獲取StatusBarHeight函式如下
/** * 獲取狀態列高度 * * @param context context * @return 狀態列高度 */ private static int getStatusBarHeight(Context context) { // 獲得狀態列高度 int resourceId = context.getResources().getIdentifier("status_bar_height","dimen","android"); return context.getResources().getDimensionPixelSize(resourceId); }
動態獲取NavigationBarHeight函式如下
/** * 獲取導航欄高度 * * @param context context * @return 導航欄高度 */ public static int getNavigationBarHeight(Context context) { int resourceId = context.getResources().getIdentifier("navigation_bar_height","android"); return context.getResources().getDimensionPixelSize(resourceId); }
4.)貼出整體java程式碼實現方式
private void initWindows() { Window window = getWindow(); int color = getResources().getColor(R.color.wechatBgColor); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); //設定狀態列顏色 window.setStatusBarColor(color); //設定導航欄顏色 window.setNavigationBarColor(getResources().getColor(R.color.footerBgColor)); ViewGroup contentView = ((ViewGroup) findViewById(android.R.id.content)); View childAt = contentView.getChildAt(0); if (childAt != null) { childAt.setFitsSystemWindows(true); } } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態列 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導航欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); //設定contentview為fitsSystemWindows ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content); View childAt = contentView.getChildAt(0); if (childAt != null) { childAt.setFitsSystemWindows(true); } //給statusbar著色 View view = new View(this); view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight(this))); view.setBackgroundColor(color); contentView.addView(view); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && useStatusBarColor) {//android6.0以後可以對狀態列文字顏色和圖示進行修改 getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); } }
到此這篇關於Android Studio 全屏沉浸式透明狀態列效果的文章就介紹到這了,更多相關Android Studio 全屏沉浸式透明狀態列內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!