0

我正在实际上向导航抽屉添加菜单项。所有项目都添加到导航视图的左侧,但是当我添加自定义菜单项目时,它将项目添加到导航视图的右侧。设置自定义抽屉菜单项的重力

我添加菜单项下面务实:

 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> 

enter image description here

编辑:我试着使用RelativeLayout而不是LinearLayout但获得相同的结果。下面是一个使用RelativeLayout的的layout_nav_menu_points代码:

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

任何代码,请建议我如何移动“点”菜单向左喜欢休息菜单项。由于

+0

为什么不使用'RelativeLayout'? – Gattsu

+0

也尝试了RelativeLayout,但获得相同的结果。 –

+0

显示相对布局的更新代码 – Gattsu

回答

0

在你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"> 

结果图像:

enter image description here

重力和layout_gravity的区别.. 井重力,设置你的内容重力(它将在哪里),而layout_gravity,设置你的布局的重力。

希望它解决了这个问题。干杯!

+0

你可以在我的问题中看到我已经试过这段代码。 –

+0

我已经改变了重力和layout_gravity值,星星标记.. –

+0

我用“开始”而不是左边的引力和layout_gravity值来支持RTL。因此,如果我使用“左”而不是“开始” –