NavigationDrawer内部的水平中心项目 [英] Horizontal center items inside NavigationDrawer

查看:94
本文介绍了NavigationDrawer内部的水平中心项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下导航抽屉菜单设置:

I have the following Navigation Drawer menu setup:

主布局 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="150dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/colorPrimaryDark"
        android:fitsSystemWindows="true"
        android:theme="@style/NavigationViewStyle"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" >
        </android.support.design.widget.NavigationView>
</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"
        android:title=""/>
</menu>

菜单项 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="match_parent"
    android:layout_height="match_parent">

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

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

我得到的结果是:

我得到的结果

相反,我希望它水平居中,在这里我做错了什么?我已经尝试过使用自定义布局的引力( menu_item.xml )以及项目进行许多修复,但似乎无济于事.

Instead I want it to be horizontally centered, what did I do wrong here? I have tried many fixes with the custom layout's gravity (menu_item.xml) as well as the items, nothing seems to work.

我不知道菜单中的app:actionLayout是否可以居中,否则请告诉我用我想要的对齐方式制作类似项目的正确方法是什么. 预先感谢.

I don't know if the app:actionLayout in the menu can be centered or not at all, if not please tell me what is the correct way to make an item like that with the alignment I want. Thanks in advance.

推荐答案

为此

menu_item.xml

menu_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:layout_gravity="center|center_horizontal"
        android:src="@drawable/menu_icon" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/image_view"
        android:layout_gravity="center_horizontal|center"
        android:gravity="center"
        android:text="Menu Title"
        android:textColor="@color/white"
        android:textSize="16sp" />
</RelativeLayout>

希望这会有所帮助

这篇关于NavigationDrawer内部的水平中心项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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