Android 狀態(tài)欄虛擬導航鍵透明效果的實現(xiàn)方法
狀態(tài)欄和虛擬導航鍵 4.4上半透明,5.0以上可以全透明
先上效果
4.4 半透明效果
5.0及以上 全透明效果
上代碼
MainActivity代碼
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 隱藏標題欄 supportRequestWindowFeature(Window.FEATURE_NO_TITLE); View root = LayoutInflater.from(this).inflate(R.layout.activity_main, null); // 或者 在界面的根層加入 android:fitsSystemWindows=”true” 這個屬性,這樣就可以讓內(nèi)容界面從 狀態(tài)欄 下方開始。 ViewCompat.setFitsSystemWindows(root, true); setContentView(root); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Android 5.0 以上 全透明 Window window = getWindow(); 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); // 狀態(tài)欄(以上幾行代碼必須,參考setStatusBarColor|setNavigationBarColor方法源碼) window.setStatusBarColor(Color.TRANSPARENT); // 虛擬導航鍵 window.setNavigationBarColor(Color.TRANSPARENT); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // Android 4.4 以上 半透明 Window window = getWindow(); // 狀態(tài)欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // 虛擬導航鍵 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } } }
activity_main.xml代碼:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> </RelativeLayout>
5.0以上的幾行代碼不是很懂,從源碼看是需要添加的,以后找到這幾個方法是做什么用的再回來注明
setStatusBarColor源碼
/** * Sets the color of the status bar to {@code color}. * * For this to take effect, * the window must be drawing the system bar backgrounds with * {@link android.view.WindowManager.LayoutParams#FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS} and * {@link android.view.WindowManager.LayoutParams#FLAG_TRANSLUCENT_STATUS} must not be set. * * If {@code color} is not opaque, consider setting * {@link android.view.View#SYSTEM_UI_FLAG_LAYOUT_STABLE} and * {@link android.view.View#SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}. * <p> * The transitionName for the view background will be "android:status:background". * </p> */ public abstract void setStatusBarColor(@ColorInt int color);
setNavigationBarColor源碼方法
/** * Sets the color of the navigation bar to {@param color}. * * For this to take effect, * the window must be drawing the system bar backgrounds with * {@link android.view.WindowManager.LayoutParams#FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS} and * {@link android.view.WindowManager.LayoutParams#FLAG_TRANSLUCENT_NAVIGATION} must not be set. * * If {@param color} is not opaque, consider setting * {@link android.view.View#SYSTEM_UI_FLAG_LAYOUT_STABLE} and * {@link android.view.View#SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}. * <p> * The transitionName for the view background will be "android:navigation:background". * </p> */ public abstract void setNavigationBarColor(@ColorInt int color);
fitsSystemWindows屬性需設(shè)置為true,否則布局會和狀態(tài)欄重疊
如圖:
兩種方式:
方式一(xml文件根布局添加屬性):
Android:fitsSystemWindows=”true”
方式二(代碼中設(shè)置):
ViewCompat.setFitsSystemWindows(rootView, true);
其實還有第三種方式解決此問題,獲取狀態(tài)欄高度,在最上設(shè)置一個等高的View
/** * 獲取狀態(tài)欄高度 * @return */ public int getStatusBarHeight() { int statusBarHeight = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { statusBarHeight = getResources().getDimensionPixelSize(resourceId); } return statusBarHeight; }
源碼地址:https://github.com/StormSunCC/MyCompatStatusBar
以上所述是小編給大家介紹的Android 狀態(tài)欄虛擬導航鍵透明效果的實現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- android 動態(tài)控制狀態(tài)欄顯示和隱藏的方法實例
- Android 去掉狀態(tài)欄的方法匯總
- Android 實現(xiàn)沉浸式狀態(tài)欄的方法
- Android自定義狀態(tài)欄顏色與應用標題欄顏色一致
- Android實現(xiàn)的狀態(tài)欄定制和修改方法
- Android 取得狀態(tài)欄、任務(wù)欄高度的小例子
- Android之沉浸式狀態(tài)欄的實現(xiàn)方法、狀態(tài)欄透明
- Android 4.4以上"沉浸式"狀態(tài)欄效果的實現(xiàn)方法
- Android編程實現(xiàn)禁止StatusBar下拉的方法
- Android開發(fā)實現(xiàn)應用層面屏蔽狀態(tài)欄的方法小結(jié)
- Android編程實現(xiàn)禁止狀態(tài)欄下拉的方法詳解
相關(guān)文章
android中關(guān)于call撥號功能的實現(xiàn)方法
這篇文章主要介紹了android中關(guān)于call撥號功能實現(xiàn)的記錄,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05Android 虛擬按鍵適配動態(tài)調(diào)整布局的方法
今天小編就為大家分享一篇Android 虛擬按鍵適配動態(tài)調(diào)整布局的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Android開發(fā)中Activity屬性設(shè)置小結(jié)
Android應用開發(fā)中會經(jīng)常遇到Activity組件的使用,下面就來講解下Activity組件。Activity的生命周期、通信方式和IntentFilter等內(nèi)容,并提供了一些日常開發(fā)中經(jīng)常用到的關(guān)于Activity的技巧和方法。通過本文,你可以進一步了接Android中Activity的運作方式。2015-05-05Java4Android開發(fā)教程(一)JDK安裝與配置
本文是Android開發(fā)系列教程的第一篇,主要為大家?guī)淼氖情_發(fā)環(huán)境的準備工作,JDK安裝與配置圖文教程,非常的詳細,有需要的朋友可以參考下2014-10-10Android Fragment的靜態(tài)注冊和動態(tài)注冊創(chuàng)建步驟
這篇文章主要介紹了Android Fragment的靜態(tài)注冊和動態(tài)注冊創(chuàng)建步驟,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02