【问题标题】:set navigation drawer on actionbar in android在android中的actionbar上设置导航抽屉
【发布时间】:2016-11-14 12:27:09
【问题描述】:

我创建了一个导航抽屉。我想在状态栏下方显示抽屉。它的工作正常,正如我所期望的那样。但抽屉在版本 5 及更高版本的状态栏上方打开。我想设置抽屉就像第二张图片一样。如何在所有版本的状态栏下方的操作(即)上设置导航抽屉。`

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="close"
   >

    <include
        layout="@layout/emp_app_bar_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="start"

        android:fitsSystemWindows="true"

        app:itemTextColor="@color/black"
        app:itemIconTint="@color/black"
        android:background="@drawable/menubg"
        app:headerLayout="@layout/emp_header"
        app:menu="@menu/activity_emp_drawer" />


</android.support.v4.widget.DrawerLayout>

`

【问题讨论】:

标签: android android-actionbar navigation-drawer


【解决方案1】:

尝试将android:fitsSystemWindows="true" 删除到您的DrawerLayout 或像android:fitsSystemWindows="false" 一样将其设为假,这可能适合您。

【讨论】:

    【解决方案2】:

    用这个替换你的代码:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout       
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    
    <include
        layout="@layout/emp_app_bar_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:itemTextColor="@color/black"
        app:itemIconTint="@color/black"
        android:background="@drawable/menubg"
        app:headerLayout="@layout/emp_header"
        app:menu="@menu/activity_emp_drawer" />
    

    【讨论】:

      【解决方案3】:

      我所做的是找到状态栏的高度并将其偏移。

      private NavigationView mNavView;
      private View mHeaderView;
      //...
      mNavView = (NavigationView) findViewById(R.id.navView);
      //...
      mHeaderView = mNavView.getHeaderView(0);
      //...
      LinearLayout mHeaderLayout = (LinearLayout) HeaderView.findViewById(R.id.headerLayout);
      //..
      mHeaderLayout.setPadding(0, getStatusBarHeight(), 0, 0);
      

      获取状态栏高度的函数是:

      public int getStatusBarHeight() {
              int result = 0;
              int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
              if (resourceId > 0) {
                  result = getResources().getDimensionPixelSize(resourceId);
              }
              return result;
      }
      

      希望对您有所帮助。

      附言。查找状态栏高度我很久以前在其他地方得到的......

      【讨论】:

      • 你的回答很好,但有一件事是如果我为 headerlayout 设置填充意味着一些文本在里面移动。所以我已经为 mNavView 设置了填充。
      • 之后它可以正常工作,但状态栏顶部显示灰色。是否可以在 mNavView 中动态设置边距而不是填充
      • 我认为如果你移动边距,它会暴露你看到的灰色背景。所以我认为填充比边距更好。顺便问一下,有些文字被移到里面是什么意思?
      • 如果你仔细观察,我改变了布局,而不是标题本身,也不是导航视图。我认为您可以尝试将所有内容包装在一个布局中,然后在该布局的顶部填充。
      • 查看发布的图片,其中包含用户名和电子邮件 ID。该电子邮件 ID 在布局内移动
      【解决方案4】:

      android:fitsSystemWindows="true" 放入您在app:headerLayout="@layout/emp_header" 中提到的emp_header

      【讨论】:

        猜你喜欢
        • 2014-06-11
        • 1970-01-01
        • 2016-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-28
        相关资源
        最近更新 更多