Android 4.4上带有半透明状态栏的导航抽屉边距

时间:2014-01-18 02:27:01

标签: android android-actionbar navigation-drawer

在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>

2 个答案:

答案 0 :(得分:2)

按照此处的代码 - 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 libary

https://github.com/jgilfelt/SystemBarTint

答案 1 :(得分:0)

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

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;
}