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

Luk*_*der 5 layout android menu navigation-drawer

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

想要的结果

图片:想要的结果

我尝试使用 actionLayout 参数实现这一点,但我似乎只能使用右侧的一些空间,而不是整个宽度:

当前结果

图片:当前结果

标题似乎占据了左侧的空间。但我想添加一个全宽的按钮,就像第一张图片一样。

我目前的代码:

...

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

...
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

Luk*_*der 1

我的解决方案现在使用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();
...
Run Code Online (Sandbox Code Playgroud)

按钮_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"/>
Run Code Online (Sandbox Code Playgroud)