Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toolbar button gravity for extended toolbar

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:

enter image description here

like image 400
Richard Avatar asked Feb 12 '15 13:02

Richard


2 Answers

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" />
like image 52
Egor Neliuba Avatar answered Nov 15 '22 08:11

Egor Neliuba


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.

like image 40
Gabriele Mariotti Avatar answered Nov 15 '22 10:11

Gabriele Mariotti