[英]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
: 以下是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 : I tried using RelativeLayout
instead of LinearLayout
but getting the same result. 编辑 :我尝试使用RelativeLayout
而不是LinearLayout
但得到相同的结果。 Below is the code of layout_nav_menu_points
using RelativeLayout
: 下面是使用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>
EDIT: Below is the code for shape_red_dot
编辑:下面是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..... 在您的nav_menu_points ..中,请务必更改您的layout_gravity和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">
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. 重力和layout_gravity之间的区别。好吧,重力,设置内容的重力(在哪里),而layout_gravity,设置布局的重力。
Hope it solves the problem. 希望它能解决问题。 Cheers! 干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.