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

2023-09-05 04:20:09 作者:帅到惊动联合国

我试着去把我背的导航图标的位置在我的扩展工具栏,如下所示:

Im trying to set the position of my back navigation icon in my extended toolbar as follows:

<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" />

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

Checking the sources of toolbar we can see the following:

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 如果不配置。然而,它看起来是这样的:

So reading this correctly, the gravity of my toolbar should already be Gravity.TOP if nothing is configured. However it looks like this:

推荐答案

我打了一下周围,并测试了几个的组合,可以说按钮的重力和安卓了minHeight 不想成为朋友。

I played around a bit and tested a couple of combinations and can say that button's gravity and android:minHeight don't want to be friends.

这应该工作的罚款:

<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" />