Android: Navigation Drawer Header Image

妖精的绣舞 提交于 2021-02-19 08:11:26

问题


I try to create a Navigation Drawer for all my Activities. I use Android Studio and generate a Navigation Drawer Activity. My other Activities extends in it to get the Navigation Drawer. I insert a Pictue with the Android Studio Feature and now exist the hdpi and xhdpi... in the "mipmap" Folder.

I change in the Navigation Header Layout Background to my inserted Image:

android:background="@mipmap/ic_lmg_bck"

nav_header_nd.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="@dimen/nav_header_height"
    android:background="@mipmap/ic_lmg_bck"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        app:srcCompat="@drawable/ic_launcher" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Nutzername"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nutzernameemail" />

</LinearLayout>

All works but the Image is blurred and to small:

Original Picture:

How it is possible to resize the Image ?


回答1:


To add the background, you need to only copy/paste your picture to the drawable folder (Don´t add the Image with "add new Image Asset").

After this you go to your: nav_header_nd.xml

And change background to:

android:background="@drawable/mypicture"



回答2:


Here is the code which I use, it is little customized navigationView:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/content_home"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<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="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:scaleType="centerCrop"
            android:layout_weight="1" />

        <LinearLayout
            android:id="@+id/ll_feedback"
            style="@style/Navigation_linear">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="44dp"
                android:src="@drawable/feedback" />

            <TextView
                style="@style/Navigation_text"
                android:text="Give us feedback" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_share_app"
            style="@style/Navigation_linear">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="44dp"
                android:src="@drawable/faq" />

            <TextView
                style="@style/Navigation_text"
                android:text="Share app" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_rate_app"
            style="@style/Navigation_linear">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="44dp"
                android:src="@drawable/rate" />

            <TextView
                style="@style/Navigation_text"
                android:text="Rate app" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_setting"
            style="@style/Navigation_linear">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="44dp"
                android:src="@drawable/setting" />

            <TextView
                style="@style/Navigation_text"
                android:text="Settings" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_notifications"
            style="@style/Navigation_linear">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="44dp"
                android:src="@drawable/notification" />

            <TextView
                style="@style/Navigation_text"
                android:text="Notifications" />
        </LinearLayout>

        <ImageView
            android:id="@+id/backNavigation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="72dp"
            android:layout_marginLeft="16dp"
            android:adjustViewBounds="true"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            android:padding="24dp"
            android:src="@drawable/arrow_left" />
    </LinearLayout>
</android.support.design.widget.NavigationView>

Try it, and let me know if it helps.



来源:https://stackoverflow.com/questions/41335614/android-navigation-drawer-header-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!