温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android应用中怎么实现一个导航键透明效果

发布时间:2020-11-26 17:18:48 来源:亿速云 阅读:183 作者:Leah 栏目:移动开发

这期内容当中小编将会给大家带来有关Android应用中怎么实现一个导航键透明效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

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,否则布局会和状态栏重叠

如图:

Android应用中怎么实现一个导航键透明效果 

两种方式:

方式一(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;
  }

上述就是小编为大家分享的Android应用中怎么实现一个导航键透明效果了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI