【问题标题】:headerLayout for navigationView not showing导航视图的 headerLayout 未显示
【发布时间】:2020-05-14 19:19:52
【问题描述】:

我正在制作一个简单的导航抽屉,但是我的 header.xml 没有显示在我的 navigation_drawer.xml 中。

我仔细检查了一切,看起来还不错。我是否错过了什么或做错了什么?

谢谢!

activity_navigation_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start"
    android:background="@color/black"
    tools:context=".NavigationDrawer"
    android:id="@+id/drawer">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/toolbar"
            android:background="@color/black"/>

    </RelativeLayout>

    <com.google.android.material.navigation.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/navigationView"
        app:menu="@menu/menu_item"
        app:headerLayout="@layout/header"
        android:background="@color/seekbar_container_color"/>



</androidx.drawerlayout.widget.DrawerLayout>

header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/activated_btn_color">


    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/brightness_icon"
        android:layout_gravity="center"
        android:tint="@color/white"
        android:layout_marginTop="20dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_txt"
        android:textSize="20sp"
        android:textColor="@color/white"
        android:layout_gravity="center"
        android:layout_marginTop="3dp"/>

</LinearLayout>

styles.xml sn-p:

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"></style>

【问题讨论】:

    标签: android xml android-layout


    【解决方案1】:

    像这样将 layout_gravity="start|left" 设置为 NavigationView

    <?xml version="1.0" encoding="utf-8"?>
    <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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:openDrawer="start"
        android:background="@color/black"
        tools:context=".NavigationDrawer"
        android:id="@+id/drawer">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/toolbar"
                android:background="@color/black"/>
    
        </RelativeLayout>
    
        <com.google.android.material.navigation.NavigationView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/navigationView"
            app:menu="@menu/menu_item"
            app:headerLayout="@layout/header"
            android:layout_gravity="start|left"
            android:background="@color/seekbar_container_color"/>
    
    
    </androidx.drawerlayout.widget.DrawerLayout>
    

    【讨论】:

      【解决方案2】:

      试试这个方法,你不需要添加任何东西。

      <?xml version="1.0" encoding="utf-8"?>
          <androidx.constraintlayout.widget.ConstraintLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
      
              <androidx.drawerlayout.widget.DrawerLayout
                  android:id="@+id/drawer_layout"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
      
                  <RelativeLayout
                      android:layout_width="match_parent"
                      android:layout_height="match_parent">
      
                      <LinearLayout
                          android:id="@+id/container_body"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:orientation="vertical" />
      
                      <include
                          android:id="@+id/toolbar"
                          layout="@layout/toolbar"
                          android:layout_height="wrap_content"
                          android:layout_width="match_parent" />
      
                  </RelativeLayout>
      
                  <fragment
                      android:id="@+id/fragment_navigation_drawer"
                      android:name="com.personalclockapp.UI.Drawer.SideDrawerFragment"
                      android:layout_width="340dp"
                      android:layout_height="match_parent"
                      android:layout_gravity="start"
                      app:layout="@layout/fragment_navigation_drawer" />
      
              </androidx.drawerlayout.widget.DrawerLayout>
      
      
      
          </androidx.constraintlayout.widget.ConstraintLayout>
      

      这是fragment_navigation_drawer.xml

      <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@color/colorPrimary">
      
          <ImageView
              android:id="@+id/closeIv"
              android:layout_width="@dimen/_30sdp"
              android:layout_height="@dimen/_30sdp"
              android:layout_margin="@dimen/_15sdp"
              android:foregroundTint="@color/white"
              android:src="@drawable/ic_close_white"
              app:layout_constraintRight_toRightOf="parent"
              app:layout_constraintTop_toTopOf="parent" />
      
          <ImageView
              android:id="@+id/logoIv"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginTop="@dimen/_50sdp"
              android:src="@drawable/ic_logo"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/closeIv" />
      
          <androidx.recyclerview.widget.RecyclerView
              android:id="@+id/drawerList"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_marginTop="@dimen/_30sdp"
              app:layout_constraintTop_toBottomOf="@+id/logoIv"
              android:scrollbars="none"/>
      
          <ImageView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:scaleType="fitXY"
              android:src="@drawable/ic_sitetrack"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintRight_toRightOf="parent" />
      
          <TextView
              android:id="@+id/logoutTv"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:fontFamily="@font/roboto_regular"
              android:text="@string/logout"
              android:padding="@dimen/_15sdp"
              android:textSize="@dimen/_12sdp"
              android:textColor="@color/white"
              android:layout_marginBottom="@dimen/_30sdp"
              android:drawableLeft="@drawable/ic_logout"
              android:drawablePadding="@dimen/_15sdp"
              android:gravity="center_vertical"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintBottom_toBottomOf="parent" />
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多