How to change header height in NavigationView?

Viewed 4659

How can I change a header view height in the NavigationView? I have next 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"/>

But NavigationView ignores all dimension attributes and shows a header with default sizes. How to fix it?

2 Answers

It's late but I found it useful.
1- try to put your ImageView in a container like FrameLayout
2- then set FrameLayout's width and height "match_parent"
3- set height of ImageView to "match_parent" too
4- add adjustViewBounds = "true" to ImageView
5- add maxHeight = "100dp" to ImageView
so this way ImageView is set to "match_parent" but it is at last 100dp long in height

<?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>
Related