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)方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(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自定義View實(shí)現(xiàn)折線圖效果
這篇文章介紹的是一個(gè)折線圖控件,用來顯示一系列的狀態(tài),并可以進(jìn)行滑動(dòng)。有需要的可以參考借鑒。2016-08-08
android中關(guān)于call撥號(hào)功能的實(shí)現(xiàn)方法
這篇文章主要介紹了android中關(guān)于call撥號(hào)功能實(shí)現(xiàn)的記錄,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
Android 虛擬按鍵適配動(dòng)態(tài)調(diào)整布局的方法
今天小編就為大家分享一篇Android 虛擬按鍵適配動(dòng)態(tài)調(diào)整布局的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Android開發(fā)中Activity屬性設(shè)置小結(jié)
Android應(yīng)用開發(fā)中會(huì)經(jīng)常遇到Activity組件的使用,下面就來講解下Activity組件。Activity的生命周期、通信方式和IntentFilter等內(nèi)容,并提供了一些日常開發(fā)中經(jīng)常用到的關(guān)于Activity的技巧和方法。通過本文,你可以進(jìn)一步了接Android中Activity的運(yùn)作方式。2015-05-05
Java4Android開發(fā)教程(一)JDK安裝與配置
本文是Android開發(fā)系列教程的第一篇,主要為大家?guī)淼氖情_發(fā)環(huán)境的準(zhǔn)備工作,JDK安裝與配置圖文教程,非常的詳細(xì),有需要的朋友可以參考下2014-10-10
Android Fragment的靜態(tài)注冊(cè)和動(dòng)態(tài)注冊(cè)創(chuàng)建步驟
這篇文章主要介紹了Android Fragment的靜態(tài)注冊(cè)和動(dòng)態(tài)注冊(cè)創(chuàng)建步驟,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Android創(chuàng)建Menu菜單實(shí)例
這篇文章主要介紹了Android創(chuàng)建Menu菜單實(shí)例,講述了Android菜單項(xiàng)的創(chuàng)建方法,在Android應(yīng)用程序開發(fā)中非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
Android判斷當(dāng)前棧頂Activity的包名代碼示例
這篇文章主要介紹了Android判斷當(dāng)前棧頂Activity的包名代碼示例,分享了相關(guān)代碼,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02

