我正在使用actionLayout标记,但它将所有内容移动到右侧。有人可以建议为什么会发生这种情况。我也尝试过不同的图标和相同的结果。
activity_main.xml中
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_saved_deals"
android:icon="@drawable/ic_attach_money_black_24dp"
android:title="Saved Deals" />
<item
android:id="@+id/nav_settings"
android:icon="@mipmap/perm_group_system_tools"
android:title="Settings" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_fb_share"
android:title=""
app:actionLayout="@layout/fb_menu_item" />
<item
android:id="@+id/nav_share_other"
android:icon="@drawable/ic_menu_share"
android:title="Share other" />
</menu>
</item>
</menu>
fb_menu_item.xml
<?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">
<ImageView
android:id="@+id/icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@mipmap/facebook" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/icon"
android:layout_toRightOf="@+id/icon"
android:gravity="center_vertical"
android:text="Line 1"
android:textColor="#000"
android:textStyle="bold" />
<TextView
android:id="@+id/subTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_toRightOf="@+id/icon"
android:text="Line 2" />
</RelativeLayout>
布局显示在屏幕的左上角,状态栏上方。
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<include
layout="@layout/fb_menu_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.design.widget.NavigationView>
答案 0 :(得分:3)
通常,需要actionLayout来设置操作视图(而不是整个项目),并且它始终与右侧对齐。而且我不确定这种对齐可能会改变。您可以在此处找到有关此https://developer.android.com/guide/topics/resources/menu-resource.html
的其他信息至于自定义项目,据我所知,最好以下列方式使用NavigationView:
PHP Fatal error: Uncaught exception 'DomainException' with message 'Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information'
我刚试过这个解决方案 - 它的确有效:
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/nav_header" />
... listview/linearlayout/just items ...
</LinearLayout>
</android.support.design.widget.NavigationView>
答案 1 :(得分:-1)
试试这个
public void addItem(){
MenuItem mainItem = navigationView.getMenu().findItem(R.id.Communicate);
SubMenu subMenu = mainItem.getSubMenu();
subMenu.add(R.id.Communicate, Menu.NONE, Menu.NONE, "Share to Facebook").setIcon(context.getResources().getDrawable(R.drawable.facebook, context.getTheme()));
}