【问题标题】:Android NavigationDrawer overlap by status barAndroid NavigationDrawer 通过状态栏重叠
【发布时间】:2016-05-27 19:39:18
【问题描述】:

我看到很多关于在导航栏顶部设置半透明状态栏的问题,如下所示:

但我想要实现的是拥有一个非半透明状态栏并使导航抽屉标题视图与操作栏高度相同。这适用于棒棒糖之前的设备(API layout_marginTop。是否有更好的解决方案可以为所有设备解决此问题?

API

API > 21:

styles.xml(v21):

<resources>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>

抽屉布局:

    <include
        layout="@layout/app_bar_main"
        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="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="false"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

我尝试将fitsSystemWindows 设置为truefalse。没有变化。

这是我初始化导航抽屉时的 hack 解决方案(@AkashBhave 也回答了这个问题):

    LinearLayout navigationHeader = (LinearLayout) navigationView.getHeaderView(0);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        // This is make sure navigation header is below status bar
        // This only required for devices API >= 21
        LinearLayout.LayoutParams layoutParams =
                (LinearLayout.LayoutParams) navigationHeader.getLayoutParams();
        layoutParams.setMargins(0, getStatusBarHeight(), 0, 0);
        navigationHeader.setLayoutParams(layoutParams);
    }

【问题讨论】:

    标签: android navigation-drawer statusbar


    【解决方案1】:

    确保fitsSystemWindows 设置为false,不仅在您的NavigationView 中,而且在您的根DrawerLayout 元素以及您的任何内容布局中。

    我在这个问题上苦苦挣扎了一段时间,直到我注意到一个 android:fitsSystemWindows="true" 在我将它包裹在 DrawerLayout 中之前已设置在我的旧根布局元素中。

    我删除了所有出现的 android:fitsSystemWindows 并且状态栏也得到了 API 21+ 的尊重。

    <android.support.v4.widget.DrawerLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/drawer_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          >
    
          <android.support.design.widget.CoordinatorLayout
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".ui.ImprintActivity">
    
            <android.support.design.widget.AppBarLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:theme="@style/AppTheme.AppBarOverlay">
    
              <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:theme="@style/AppTheme.Toolbar"
                app:titleTextAppearance="@style/Toolbar.TitleText"
                app:contentInsetStartWithNavigation="0dp"
                app:contentInsetStart="0dp"
                >
    
                <include layout="@layout/toolbar_logo_small"/>
    
              </android.support.v7.widget.Toolbar>
    
            </android.support.design.widget.AppBarLayout>
    
            <include layout="@layout/content_imprint"/>
    
          </android.support.design.widget.CoordinatorLayout>
    
          <android.support.design.widget.NavigationView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/navigation_view"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_gravity="start"
            app:headerLayout="@layout/navigation_view_header"
            app:menu="@menu/navigation_view_menu"
            android:background="@color/primary_light"
            />
    
        </android.support.v4.widget.DrawerLayout>
    

    【讨论】:

      【解决方案2】:

      我认为您应该为高于 API 21 + 的设备设置一个 margin_top 属性。您可以通过以下方式检索系统 API 级别:

      Build.VERSION_CODES.LOLLIPOP
      

      您可以通过以下方式对其进行检查和设置:

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
          params.setMargins(left, top, right, bottom);
          yourbutton.setLayoutParams(params);
      }
      

      希望对你有帮助!

      【讨论】:

      • 谢谢。这就是我所做的。但我认为可能有更优雅的解决方案。
      • 我认为没有。我试图寻找一个,但我没有找到一个。我认为这是最好的(而且还不错)。
      【解决方案3】:

      您可以尝试在抽屉打开时将状态栏颜色设置为透明 -

      getWindow().setStatusBarColor(Color.TRANSPARENT);
      

      【讨论】:

        【解决方案4】:

        试试这个android:fitsSystemWindows="false"

        <androidx.drawerlayout.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="false"
        tools:openDrawer="start">
        
        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer" />
        

        【讨论】:

          【解决方案5】:

          添加 -> 在DrawerLayout:

          android:fitsSystemWindows="true"
          

          NavigationView:

          android:fitsSystemWindows="false"
          

          【讨论】:

            猜你喜欢
            • 2016-05-10
            • 1970-01-01
            • 2015-05-02
            • 1970-01-01
            • 1970-01-01
            • 2019-04-24
            • 1970-01-01
            • 1970-01-01
            • 2011-02-20
            相关资源
            最近更新 更多