国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 狀態欄虛擬導航鍵透明效果的實現方法

2019-12-12 03:25:34
字體:
來源:轉載
供稿:網友

狀態欄和虛擬導航鍵 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” 這個屬性,這樣就可以讓內容界面從 狀態欄 下方開始。    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);      // 狀態欄(以上幾行代碼必須,參考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();      // 狀態欄      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屬性需設置為true,否則布局會和狀態欄重疊

如圖:

這里寫圖片描述 

兩種方式:

方式一(xml文件根布局添加屬性):

Android:fitsSystemWindows=”true”

方式二(代碼中設置):

ViewCompat.setFitsSystemWindows(rootView, true);

其實還有第三種方式解決此問題,獲取狀態欄高度,在最上設置一個等高的View

/**   * 獲取狀態欄高度   * @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 狀態欄虛擬導航鍵透明效果的實現方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广德县| 铅山县| 潢川县| 柳州市| 武平县| 正镶白旗| 鄂州市| 阳城县| 十堰市| 天气| 九台市| 拜泉县| 阜南县| 上犹县| 田阳县| 汉寿县| 长汀县| 永德县| 宁明县| 勃利县| 谢通门县| 全椒县| 息烽县| 繁昌县| 绥江县| 平乡县| 集贤县| 南充市| 青神县| 红河县| 宾阳县| 山东省| 博客| 那坡县| 健康| 五河县| 霞浦县| 九江县| 桐梓县| 鄂州市| 林口县|