最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 狀態(tài)欄虛擬導(dǎo)航鍵透明效果的實(shí)現(xiàn)方法

 更新時(shí)間:2017年03月10日 10:03:55   作者:Storm_Sun_cc  
這篇文章主要介紹了Android 狀態(tài)欄虛擬導(dǎo)航鍵透明效果的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

狀態(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)欄 下方開(kā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è)方法是做什么用的再回來(lái)注明

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í)還有第三種方式解決此問(wèn)題,獲取狀態(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ì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

海伦市| 榆树市| 惠东县| 乐至县| 福清市| 炎陵县| 南投市| 冕宁县| 行唐县| 白沙| 东光县| 常熟市| 平果县| 明溪县| 吉安县| 南投市| 久治县| 漳州市| 邵阳市| 安顺市| 遵义县| 大悟县| 南安市| 定南县| 青岛市| 大连市| 东安县| 海口市| 西吉县| 株洲县| 高碑店市| 五原县| 绥阳县| 潍坊市| 乃东县| 麻江县| 沙雅县| 乐清市| 江孜县| 靖州| 上犹县|