简体   繁体   中英

Set gravity of custom navigation drawer menu item

I am adding menu items to navigation drawer pragmatically. All the items are added to the left of navigation view but when I add custom menu item, it adds the item to the right of navigation view.

I am adding menu items pragmatically as below:

        Menu menu = mNavigationView.getMenu();
        if (menu == null) return;

        // add home menu
        menu.add(0, 1, Menu.NONE, "Home");

        // add refer menu
        menu.add(0, 2, Menu.NONE, "Refer and Earn");

        // add points menu
        MenuItem menuItem = menu.add(0, 3, Menu.NONE, null);
        menuItem.setActionView(R.layout.layout_nav_menu_points);

        // add settings menu
        menu.add(0, 4, Menu.NONE, "Settings");

        // add about us menu
        menu.add(0, 5, Menu.NONE, "About us");

        // add logout menu
        menu.add(0, 6, Menu.NONE, "Logout");

Below is the code of layout_nav_menu_points :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:gravity="start|center_vertical"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Points"
            android:textColor="@android:color/black"
            android:textSize="@dimen/font_14" />

        <ImageView
            android:layout_width="@dimen/dimen_5"
            android:layout_height="@dimen/dimen_5"
            android:layout_marginLeft="@dimen/dimen_2"
            android:layout_marginRight="@dimen/dimen_2"
            android:contentDescription="@string/app_name"
            android:src="@drawable/shape_red_dot" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="-2dp"
        android:layout_marginLeft="@dimen/dimen_2"
        android:layout_marginRight="@dimen/dimen_2"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textColor="@color/textColorPrimary"
            android:textSize="@dimen/font_12" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dimen_2"
            android:layout_marginRight="@dimen/dimen_2"
            android:text="pt"
            android:textColor="@color/textColorPrimary"
            android:textSize="@dimen/font_12" />
    </LinearLayout>

</LinearLayout>

Getting below result: 在此处输入图片说明

EDIT : I tried using RelativeLayout instead of LinearLayout but getting the same result. Below is the code of layout_nav_menu_points using RelativeLayout :

<?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="match_parent"
    android:layout_gravity="start"
    android:gravity="start|center_vertical"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ll_points"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Points"
            android:textColor="@android:color/black"
            android:textSize="@dimen/font_14" />

        <ImageView
            android:layout_width="@dimen/dimen_5"
            android:layout_height="@dimen/dimen_5"
            android:layout_marginLeft="@dimen/dimen_2"
            android:layout_marginRight="@dimen/dimen_2"
            android:contentDescription="@string/app_name"
            android:src="@drawable/shape_red_dot" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="-2dp"
        android:layout_below="@id/ll_points"
        android:layout_marginLeft="@dimen/dimen_2"
        android:layout_marginRight="@dimen/dimen_2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textColor="@color/textColorPrimary"
            android:textSize="@dimen/font_12" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dimen_2"
            android:layout_marginRight="@dimen/dimen_2"
            android:text="pt"
            android:textColor="@color/textColorPrimary"
            android:textSize="@dimen/font_12" />
    </LinearLayout>

</RelativeLayout>

EDIT: Below is the code for shape_red_dot

 <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">

        <solid
            android:color="#EC2027" />

        <size
            android:height="10dp"
            android:width="10dp" />
    </shape>

Anyone please suggest me how to move that "point" menu to the left like rest of the menu items. Thanks

In your nav_menu_points.. Please do change your layout_gravity and gravity value.....

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

    *** CHANGED CODE
    android:layout_gravity="left"
    android:gravity="left|center_horizontal"
    *** CHANGED CODE ENDS

    android:orientation="vertical">

Result Image:

在此处输入图片说明

Difference between gravity and layout_gravity.. Well gravity, set your contents gravity (where will it be) whereas, layout_gravity, set your layout's gravity.

Hope it solves the problem. Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM