Android 狀態(tài)欄虛擬導(dǎo)航鍵透明效果的實(shí)現(xiàn)方法
狀態(tài)欄和虛擬導(dǎo)航鍵 4.4上半透明,5.0以上可以全透明
先上效果
4.4 半透明效果

5.0及以上 全透明效果

上代碼
MainActivity代碼
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 隱藏標(biāo)題欄
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
View root = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
// 或者 在界面的根層加入 android:fitsSystemWindows=”true” 這個(gè)屬性,這樣就可以讓內(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);
// 虛擬導(dǎo)航鍵
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);
// 虛擬導(dǎo)航鍵
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以上的幾行代碼不是很懂,從源碼看是需要添加的,以后找到這幾個(gè)方法是做什么用的再回來注明
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,否則布局會(huì)和狀態(tài)欄重疊
如圖:
兩種方式:
方式一(xml文件根布局添加屬性):
Android:fitsSystemWindows=”true”
方式二(代碼中設(shè)置):
ViewCompat.setFitsSystemWindows(rootView, true);
其實(shí)還有第三種方式解決此問題,獲取狀態(tài)欄高度,在最上設(shè)置一個(gè)等高的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)欄虛擬導(dǎo)航鍵透明效果的實(shí)現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- android 動(dòng)態(tài)控制狀態(tài)欄顯示和隱藏的方法實(shí)例
- Android 去掉狀態(tài)欄的方法匯總
- Android 實(shí)現(xiàn)沉浸式狀態(tài)欄的方法
- Android自定義狀態(tài)欄顏色與應(yīng)用標(biāo)題欄顏色一致
- Android實(shí)現(xiàn)的狀態(tài)欄定制和修改方法
- Android 取得狀態(tài)欄、任務(wù)欄高度的小例子
- Android之沉浸式狀態(tài)欄的實(shí)現(xiàn)方法、狀態(tài)欄透明
- Android 4.4以上"沉浸式"狀態(tài)欄效果的實(shí)現(xiàn)方法
- Android編程實(shí)現(xiàn)禁止StatusBar下拉的方法
- Android開發(fā)實(shí)現(xiàn)應(yīng)用層面屏蔽狀態(tài)欄的方法小結(jié)
- Android編程實(shí)現(xiàn)禁止?fàn)顟B(tài)欄下拉的方法詳解
相關(guān)文章
Android貝塞爾曲線實(shí)現(xiàn)加入購物車拋物線動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android貝塞爾曲線實(shí)現(xiàn)加入購物車拋物線動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
Android編程實(shí)現(xiàn)Toast自定義布局簡單示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)Toast自定義布局的方法,結(jié)合簡單實(shí)例形式分析了Toast自定義布局的實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
Android webview打開本地圖片上傳實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android webview打開本地圖片上傳的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android用RecyclerView實(shí)現(xiàn)圖標(biāo)拖拽排序以及增刪管理
這篇文章主要介紹了Android用RecyclerView實(shí)現(xiàn)圖標(biāo)拖拽排序以及增刪管理的方法,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
Android 自定義驗(yàn)證碼輸入框的實(shí)例代碼(支持粘貼連續(xù)性)
這篇文章主要介紹了Android 自定義驗(yàn)證碼輸入框的實(shí)例代碼(支持粘貼連續(xù)性),代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
解決Android橫豎屏切換數(shù)據(jù)丟失問題的方法
這篇文章主要為大家詳細(xì)介紹了Android橫豎屏切換數(shù)據(jù)丟失問題的解決方法,感興趣的小伙伴們可以參考一下2016-05-05

