Установить гравитацию пользовательского элемента меню навигационного ящика

Я прагматично добавляю пункты меню в панель навигации. Все элементы добавляются слева от представления навигации, но когда я добавляю пользовательский элемент меню, он добавляет элемент справа от представления навигации.

Я добавляю пункты меню прагматично, как показано ниже:

        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");

Ниже приведен код 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>

Получение результата ниже: введите здесь описание изображения

EDIT: я пытался использовать RelativeLayout вместо LinearLayout, но получил тот же результат. Ниже приведен код layout_nav_menu_points с использованием 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>

РЕДАКТИРОВАТЬ: Ниже приведен код для 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>

Кто-нибудь, пожалуйста, предложите мне, как переместить это «точечное» меню влево, как и остальные пункты меню. Спасибо


person Geeky Singh    schedule 05.10.2016    source источник
comment
почему бы тебе не использовать RelativeLayout ?   -  person Maveňツ    schedule 05.10.2016
comment
Пробовал RelativeLayout, но получил тот же результат.   -  person Geeky Singh    schedule 05.10.2016
comment
показать обновленный код относительной компоновки   -  person Maveňツ    schedule 05.10.2016
comment
Обновлен мой вопрос с реализацией RelativeLayout.   -  person Geeky Singh    schedule 05.10.2016
comment
stackoverflow.com/questions/32613437/   -  person suku    schedule 05.10.2016
comment
Я получаю предварительный просмотр, подобный этому... вы не предоставили свой полный код   -  person Maveňツ    schedule 05.10.2016


Ответы (1)


В вашем nav_menu_points.. Пожалуйста, измените значение layout_gravity и гравитации.....

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

Изображение результата:

введите здесь описание изображения

Разница между гравитацией и layout_gravity. Гравитация, установите гравитацию вашего содержимого (где она будет), тогда как layout_gravity, установите гравитацию вашего макета.

Надеюсь, это решит проблему. Ваше здоровье!

person O_o    schedule 05.10.2016
comment
Вы можете видеть в моем вопросе, что я уже пробовал этот код. - person Geeky Singh; 05.10.2016
comment
Я изменил значения гравитации и layout_gravity, отмеченные внутри звездочек. - person O_o; 05.10.2016
comment
Я использовал start вместо left для гравитации и значение layout_gravity для поддержки RTL. Так что это будет то же самое, если я использую left вместо start - person Geeky Singh; 05.10.2016
comment
@GaganSingh, использовал мое изображение, чтобы воссоздать сценарий в моем конце. вышло то. - person O_o; 05.10.2016
comment
Это просто предварительный просмотр макета. Я получаю тот же предварительный просмотр, но когда я добавляю этот макет в меню навигации, он появляется справа, а не слева. Попробуйте установить этот макет для пункта меню навигации и посмотрите результат. - person Geeky Singh; 05.10.2016