如何在NavigationView中更改页眉高度?

3
我该如何更改NavigationView中标题视图的高度?我有以下xml代码:activity_main.xml
<android.support.v4.widget.DrawerLayout
    android:id="@+id/dlDrawerContainer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".activity.MainActivity"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <!-- Some data -->

    <!-- Drawer -->
    <android.support.design.widget.NavigationView
        android:fitsSystemWindows="true"
        android:id="@+id/nvNavigation"
        android:layout_width="wrap_content"
        android:maxWidth="400dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header"/>

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

navigation_header.xml

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
           android:src="@drawable/ic_header"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:maxWidth="400dp"
           android:maxHeight="100dp"
           android:scaleType="matrix"/>

但是NavigationView忽略了所有的尺寸属性,显示一个带有默认大小的标题。该如何解决?

2个回答

4
你需要修改你的 navigation_header.xml 中的 layout_height 值。
例如,这将会把头部高度改为100dp:
<?xml version="1.0" encoding="utf-8"?>
<ImageView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_header"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:maxWidth="400dp"
    android:maxHeight="100dp"
    android:scaleType="matrix"/>

1
如何在ImageView中使用wrap_content?它与NavigationView不兼容。我的ImageView填充了最大父级高度。 - Denis Sologub
抱歉,我无法理解您想要实现什么。您可以编辑您的问题并澄清期望的结果,最好附上一张图片以便更好地说明。 - Mattia Maestrini

0

虽然已经很晚了,但我觉得这个很有用。
1- 尝试将您的ImageView放入容器中,例如FrameLayout
2- 然后设置FrameLayout的宽度和高度为"match_parent"
3- 将ImageView的高度也设置为"match_parent"
4- 在ImageView中添加adjustViewBounds = "true"
5- 在ImageView中添加maxHeight = "100dp"
这样,ImageView被设置为"match_parent",但其高度至少为100dp

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

    <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:srcCompat="@mipmap/ic_launcher"
            android:id="@+id/imageView"
            android:adjustViewBounds="true" 
            android:maxHeight="100dp"/>
</FrameLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接