扩展工具栏的工具栏按钮重力

Toolbar button gravity for extended toolbar

我正在尝试按如下方式设置扩展工具栏中后退导航图标的位置:

<android.support.v7.widget.Toolbar  xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:theme="@style/Toolbar"
    android:minHeight="@dimen/action_bar_height"
    app:buttonGravity="top"
    android:gravity="top"
    android:background="?attr/colorPrimary" />

检查工具栏的来源,我们可以看到以下内容:

private void ensureNavButtonView() {
    if (mNavButtonView == null) {
        mNavButtonView = new ImageButton(getContext(), null,
            R.attr.toolbarNavigationButtonStyle);
        final LayoutParams lp = generateDefaultLayoutParams();
        lp.gravity = GravityCompat.START | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
        mNavButtonView.setLayoutParams(lp);
    }
}

其中 mButtonGravity 是通过

分配的
mButtonGravity = a.getInteger(R.styleable.Toolbar_buttonGravity, Gravity.TOP);

所以正确阅读,如果没有配置,我的工具栏的重力应该已经是 Gravity.TOP。然而它看起来像这样:

我玩了一下,测试了几个组合,可以说按钮的重力和 android:minHeight 不想成为朋友。

这应该可以正常工作:

<android.support.v7.widget.Toolbar  xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_height="@dimen/action_bar_height"
    android:layout_width="match_parent"
    app:theme="@style/Toolbar"
    android:minHeight="?attr/actionBarSize"
    android:gravity="top"
    android:background="?attr/colorPrimary" />

如果您想要顶部的图标和底部的标题,您可以尝试这样的操作:

<Toolbar
    android:id="@+id/toolbar"
    android:layout_height="128dp"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:gravity="bottom" />

使用属性android:minHeight作为标准actionBarSize,按钮和动作位于顶部。在使用属性 android:gravity 时,您可以将标题的位置设置在底部。