【发布时间】:2017-02-13 12:37:28
【问题描述】:
我正在实用地向导航抽屉添加菜单项。所有项目都添加到导航视图的左侧,但是当我添加自定义菜单项时,它会将项目添加到导航视图的右侧。
我正在务实地添加菜单项,如下所示:
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>
编辑:我尝试使用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>
请任何人建议我如何将该“点”菜单像其他菜单项一样向左移动。谢谢
【问题讨论】:
-
你为什么不用
RelativeLayout? -
也尝试过RelativeLayout,但得到了相同的结果。
-
显示相关布局的更新代码
-
用RelativeLayout 实现更新了我的问题。
标签: android navigation-drawer android-design-library navigationview