Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

android - Horizontal center items inside NavigationDrawer

I have the following Navigation Drawer menu setup:

Main layout 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>

My menu 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 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>

The result I get is this:

Result i am getting

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this for the

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>

Hope this helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...