【问题标题】:How to customise Navigation Drawer item height如何自定义导航抽屉项目高度
【发布时间】:2017-06-20 05:35:19
【问题描述】:

大家好,我正在尝试更改导航抽屉项目的高度,但我不知道怎么做,我到处寻找并尽了最大努力,但似乎没有任何效果..

这是我的导航抽屉项目,它被定制为在该图像下有一个文本,但我希望该图像更大一点。

我期待的结果是这样的:

每个项目的高度约为 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"/>

..我得到这个结果:

如你所见,我的图片比商品大..

【问题讨论】:

  • 您可以自定义导航布局
  • 要实现这一点,只需创建一个自定义布局。
  • @Anas 你试过我的答案吗...??

标签: android xml android-layout navigation-drawer


【解决方案1】:

将此添加到您的样式.xml

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

并且您在抽屉布局 shd 中的导航视图包含类似这样的主题

<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" />

【讨论】:

  • 这是完美的工作,谢谢,还有一件事,我如何将项目居中,因为它保持在右侧..
  • 为此您必须修改您的项目 xml,使线性布局宽度匹配父 & 并使文本和图像视图的布局重力居中水平
  • 你能把 xml 代码贴出来吗……或者把它作为另一个 qsn n 分享链接.. 会检查..
  • 与问题相同,我的自定义布局是第一个代码:menu_item.xml 我只删除了gravitymargins
  • 如果它与问题中的相同,它将不起作用...我告诉过您更改父视图的宽度并更改子视图的重力
【解决方案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 中,结果是一样的
  • 为菜单项创建一个custom_view,只需通过findViewById获取java文件中的每个项目,并在每个项目上设置点击列表
  • 谢谢它的工作,但它出现在导航菜单的标题和菜单的左侧,我希望它在中心
猜你喜欢
  • 2023-03-08
  • 2017-08-30
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多