如何将按钮添加到导航抽屉菜单?

How to add buttons to navigation drawer menu?

我想在抽屉式导航菜单中添加一个按钮,如下所示:

想要的结果:

我尝试使用 actionLayout 参数实现此目的,但我似乎只能在右侧使用一些 space,而不是整个宽度:

当前结果:

标题好像占了左边的space。 但是我想像第一张图片一样添加一个全宽的按钮。

我当前的代码:

...

<item
                    android:id="@+id/nav_login"
                    android:title=""
                    app:actionLayout="@layout/button_login"
                    app:showAsAction="ifRoom"/>

...

button_login.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="#0000ff"
    android:text="Login"
    android:textColor="#ffffff"
    android:layout_height="match_parent" />

抽屉中的操作视图旨在显示此 "small" 菜单项右侧的附加视图,因此它的大小会受到限制。

您可以添加所需的按钮作为某种页脚,如下所示:

<android.support.design.widget.NavigationView
    android:id="@+id/drawer"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/drawer">
    <Button
       android:id="@+id/footer_item_1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_gravity="bottom"
       android:text="Footer Button 1" />
</android.support.design.widget.NavigationView>

请注意,如果您想要更多视图,您可以将所有想要的内容作为子视图 - 只需在此处添加 LinearLayout 并将所有其他视图作为其子视图。

我的解决方案现在使用 MaterialDrawer Library

我刚刚做了一个快速测试,它解决了问题:

代码:

...
Drawer drawer = new DrawerBuilder()
                .withActivity(this)
                .withToolbar(findViewById(R.id.toolbar))
                .addDrawerItems(
                        new PrimaryDrawerItem().withName("Entry 1"),
                        new PrimaryDrawerItem().withName("Entry 2"),
                        new AbstractDrawerItem() {
                            @Override
                            public RecyclerView.ViewHolder getViewHolder(View v) {
                                return new RecyclerView.ViewHolder(v) {
                                };
                            }

                            @Override
                            public int getType() {
                                return 0;
                            }

                            @Override
                            public int getLayoutRes() {
                                return R.layout.button_a;
                            }

                            @Override
                            public Object withSubItems(List subItems) {
                                return null;
                            }

                            @Override
                            public Object withParent(IItem parent) {
                                return null;
                            }
                        },
                        new PrimaryDrawerItem().withName("Entry 3")
                )
                .build();
...

button_a.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_width="match_parent"
        android:text="Login"
        android:layout_height="50dp"/>