如何自定义导航抽屉项目的高度 [英] How to customise Navigation Drawer item height

查看:91
本文介绍了如何自定义导航抽屉项目的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试更改导航抽屉项目的高度,但我不知道如何,我到处都看过并且尽了最大的努力,但是似乎没有任何效果.

Hello guys I'm trying to change the height of my Navigation Drawer items and I don't know how, I've looked everywhere and did my best but nothing seems to work..

这是我的导航抽屉"项,该项被定制为在该图像下具有文本,但我也希望该图像更大一些.

This is my Navigation Drawer item which is customised to have a text under that image but also I want that image to be a little bigger.

我期待的结果是这样:

每个项目的高度大约为75dp,我也希望菜单的宽度更短,以使该图像看起来更美观.

With around 75dp of height for each item and also I want the menu to be shorter on the width to give that look in that image.

我正在为我的项目使用自定义布局,我将其命名为: menu_item.xml

I am using a custom layout for my items of course, which I named: menu_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_margin="10dp">

    <ImageView
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:src="@drawable/menu_icon"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Menu Title"
        android:textSize="16sp"
        android:textColor="@color/white" />

</LinearLayout>

我的 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

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

    <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:background="@color/colorPrimaryDark"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

,最后是菜单; activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto" >

        <item
            android:id="@+id/nav_profile"
            app:actionLayout="@layout/menu_item"/>
</menu>

任何想法如何实现我想要的?预先感谢

Any ideas how to achieve what I want? Thanks in advance

当我在自定义项目中按如下方式放大图像时:

When I enlarge the image in my custom item as follows:

这是在 menu_item.xml

<ImageView
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:src="@drawable/menu_icon"/>

..我得到以下结果:

..I get this result:

如您所见,我的图像大于该项目.

As you see my image is bigger than the item..

推荐答案

将此添加到您的styles.xml

add this to your styles.xml

<style name="NavigationTheme" parent="AppTheme">
        <item name="listPreferredItemHeightSmall">60dp</item><!-- menu item height- vary as u need -->
    </style>

并且您在抽屉式布局中的导航视图中将包含该主题

and your navigation view in drawer layout shd include that theme lik this

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:theme="@style/NavigationTheme"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

这篇关于如何自定义导航抽屉项目的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆