如何自定义导航抽屉项目的高度

10

大家好,我想要改变我的导航抽屉(Navigation Drawer)中选项的高度,但我不知道如何操作。我已经在各个地方寻找并尽力而为,但似乎没有什么效果。

Result

这是我自定义的导航抽屉选项,其中有一个图像下面跟着一段文本,但我想要这个图像再大一点。

我期望得到的结果是:

Menu Items

每个选项的高度为约75dp,并且我希望菜单的宽度更短,以呈现上述图片中的效果。

当然,我正在使用自定义布局,布局文件名为: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>

有什么办法可以实现我想要的吗?提前谢谢。
编辑:当我放大自定义项目中的图像如下所示:
这是在menu_item.xml中。
<ImageView
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:src="@drawable/menu_icon"/>

我得到了这个结果:

result2

如你所见,我的图片比项目还要大。

1
你可以为导航制作自定义布局。 - Aashutosh Kumar
要实现这个,只需创建一个自定义布局。 - Rameshbabu
@Anas,你试过我的答案了吗?? - Moulesh
2个回答

40
将此添加到您的styles.xml中。
<style name="NavigationTheme" parent="AppTheme">
        <item name="listPreferredItemHeightSmall">60dp</item><!-- menu item height- vary as u need -->
    </style>

我不知道。
<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" />

你需要修改你的item xml,将线性布局的宽度设置为match parent,并将文本和图像视图的布局重心设置为水平居中。 - Moulesh
1
你能发布XML代码吗?或者将其作为另一个问题发布并分享链接。我会检查的。 - Moulesh
如果和问题中的一样,它是不会起作用的...我告诉你要更改父级的宽度并更改子视图的重力。 - Moulesh
我已经尝试过,但没有成功。看看菜单项,也许我不应该把它作为actionLayout? - siriuseteor77
我创建了一个问题:https://stackoverflow.com/questions/44690957/horizontal-center-items-inside-navigationdrawer - siriuseteor77
显示剩余2条评论

2
创建自定义布局
 <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

    <include layout="@layout/custom_view"/>

</android.support.design.widget.NavigationView>

你能再解释一下吗?我尝试将我的 menu_item.xml 包含在 NavigationView 中,结果还是一样的。 - siriuseteor77
为菜单项制作一个自定义视图,然后在Java文件中通过findViewById获取每个项目并为每个项目设置点击监听器。 - Aashutosh Kumar
谢谢,它起作用了,但它出现在导航菜单的标题上方和菜单左侧,我希望它在中心。 - siriuseteor77

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