【问题标题】:Toolbar button gravity for extended toolbar扩展工具栏的工具栏按钮重力
【发布时间】:2015-02-12 13:13:03
【问题描述】:

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

<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。然而它看起来像这样:

【问题讨论】:

  • 请在此处粘贴整个 xml

标签: android android-layout android-support-library android-toolbar


【解决方案1】:

我玩了一下,测试了几个组合,可以说按钮的重力和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" />

【讨论】:

  • +1 这是正确答案。不要让minHeight 控制您的工具栏,它会搞砸重力测量。始终将minHeight 设置为默认大小,并将扩展侧设置为布局高度。
【解决方案2】:

如果你想要顶部的图标和底部的标题,你可以试试这样:

<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属性时,可以设置标题的位置在底部。

【讨论】:

  • 如何在后退箭头旁边放置标题?
猜你喜欢
  • 2017-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多