【问题标题】:NavigationDrawer margin on Android 4.4 with Translucent Status Bar带有半透明状态栏的 Android 4.4 上的 NavigationDrawer 边距
【发布时间】:2014-04-23 09:20:02
【问题描述】:

在 Android 4.4 上,当使用带有 ActionBarCompat 和 NavigationDrawer 的半透明状态栏选项时,NavigationDrawer 列表视图与 ActionBar 略有重叠。它适用于我测试过的所有其他 API 级别。

据我所知,这是因为marginTop 选项从屏幕顶部而不是操作栏顶部(状态栏底部)开始。 状态栏的底部。有一个偏移量

有解决方法吗?我正在使用示例布局并将marginTop 当前设置为?attr/actionBarSize

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     The drawer is given a fixed width in dp and extends the full height of
     the container. A solid background is used for contrast
     with the content view. -->
    <ListView
        android:id="@+id/drawer_list"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="@drawable/bg_app" />
</android.support.v4.widget.DrawerLayout>

【问题讨论】:

  • 你找到解决办法了吗?

标签: android android-actionbar navigation-drawer


【解决方案1】:

按照此处的代码进行操作 - https://github.com/afollestad/kitkat-transparency-demo

这让您可以设置半透明状态和导航栏,以及 NavigationDrawer。

如下覆盖 NavigationDrawer 中的 onViewCreated -

@Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
    SystemBarTintManager tintManager = new SystemBarTintManager(getActivity());
    SystemBarTintManager.SystemBarConfig config = tintManager.getConfig();
    view.setPadding(0, config.getPixelInsetTop(true), config.getPixelInsetRight(), config.getPixelInsetBottom());
  }

这是假设您正在使用 SystemBarTint 库

https://github.com/jgilfelt/SystemBarTint

【讨论】:

    【解决方案2】:

    最后,我不得不找到状态栏高度并手动设置边距。

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      DrawerLayout.LayoutParams lp = (DrawerLayout.LayoutParams) mDrawerList.getLayoutParams();
      lp.setMargins(0, lp.topMargin + getStatusBarHeight(), 0, 0);
      mDrawerList.setLayoutParams(lp);
    }
    
    public int getStatusBarHeight() {
      int result = 0;
      int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
      if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
      }
      return result;
    }
    

    【讨论】:

    • 为什么?我不需要这样做,但是我得到了半透明的状态栏和导航栏以及 NavigationDrawer,其中的 List 设置在操作栏下方。在此处查看屏幕截图 - bit.ly/1nFHBD9bit.ly/1nFHCak
    • 这种实现的唯一好处是避免了依赖。如果你无论如何都要使用那个库,那么那个更有意义。我最初使用了这种方法,但不希望库在那里,因为我并没有真正使用它。
    • 明白了。说得通。谢谢
    猜你喜欢
    • 2014-01-01
    • 2014-12-15
    • 1970-01-01
    • 2017-07-07
    • 2017-12-24
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    相关资源
    最近更新 更多