NavigationDrawer中的水平中心项

时间:2017-06-22 05:37:50

标签: android xml navigation-drawer

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

主要布局 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>

我得到的结果是:

Result i am getting

相反,我希望它是水平居中的,我在这里做错了什么?我已尝试使用自定义布局的重力( menu_item.xml )以及项目进行了多次修复,似乎没有任何效果。

我不知道菜单中的app:actionLayout是否可以居中,如果没有,请告诉我使用我想要的对齐方式制作这样的项目的正确方法是什么。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

尝试使用

  

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>

希望这有帮助

答案 1 :(得分:-1)

试试这个,menu_item.xml你可以将引力设置为center_horizo​​ntal。

相关问题