Toolbar button gravity for extended toolbar

丶灬走出姿态 提交于 2019-12-04 13:42:36

问题


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);
    }
}

Where mButtonGravity is assigned via

mButtonGravity = a.getInteger(R.styleable.Toolbar_buttonGravity, 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:


回答1:


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.

This should work fine:

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



回答2:


If you want the icon on the top and the title on the bottom you can try something like this:

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

Using the attribute android:minHeight as the standard actionBarSize,the buttons and actions are positioned in the top. While using the attribute android:gravity you can set the position of the title at the bottom.



来源:https://stackoverflow.com/questions/28478384/toolbar-button-gravity-for-extended-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!