android - 导航视图的 headerLayout 未显示
问题描述
我正在制作一个简单的导航抽屉,但是我的 header.xml 没有出现在我的 navigation_drawer.xml 中。
我加倍检查,一切似乎都还好。我错过了什么或做错了什么?
谢谢!
活动导航抽屉.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>
样式.xml片段:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"></style>
解决方案
试试这种方式,你不需要添加任何其他东西。
<?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>
推荐阅读
- string - Freemarker ISO字符串到带有时区的日期时间
- python - 2D 切片混乱
- arrays - sll 的目的是什么,并在翻译后的 MIPS 代码中添加?
- google-apps-script - Google 表格:如何将图像标题自动填充到单元格列中(不是图像)?
- python - “TypeError: unhashable type: 'set'” 用于 python discord bot
- c# - C#检查日期时间列表是否包含链接日期并转换为字符串
- java - 使用 for 和 while 循环对序列求和
- azure-ad-b2c - 如何对 Azure AD B2C 自定义策略中的 RESTful 终结点响应进行故障排除
- javascript - 无法匹配除 ' 和 " 之外的任何字符的正则表达式
- javascript - Javascript:如何遍历值数组并创建表行