How to align menu item to center_vertical after changed the height of toolbar

android toolbar menuitem

140 просмотра

1 ответ

After changed(decrease) the height of the toolbar(wrap_content to 40dp), the title is still center_vertical, but the menu item bias to bottom. How to fix? Thanks.

enter image description here

Code:

toolbar.inflateMenu(R.menu.menu_toolbar);

XML: (if I don't change layout_height="40dp", then no problem)

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_centerInParent="true"
    android:background="?attr/colorAccent"
    android:minHeight="?attr/actionBarSize"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp"
    app:titleTextAppearance="@style/Toolbar.TitleText" />

MENU ITEM XML:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:gravity="end|center">
<item
    android:id="@+id/action_add"
    android:icon="@drawable/ic_add_black_24dp"
    android:orderInCategory="10"
    android:title="添加"
    app:showAsAction="always" />
<item
    android:id="@+id/action_delete"
    android:icon="@drawable/ic_remove_black_24dp"
    android:orderInCategory="10"
    android:title="删除"
    app:showAsAction="always" />

<item
    android:id="@+id/action_start"
    android:icon="@drawable/ic_play_arrow_black_24dp"
    android:orderInCategory="10"
    android:title="开启"
    app:showAsAction="always" />

<item
    android:id="@+id/action_stop"
    android:icon="@drawable/ic_stop_black_24dp"
    android:orderInCategory="10"
    android:title="停止"
    app:showAsAction="always" />

</menu>

[Toolbar]

Автор: qcm217 Источник Размещён: 08.11.2019 11:19

Ответы (1)


0 плюса

Решение

Your issue is default behavior of Toolbar.There is no official api to set the icons in vertically center.

Below code is un-official way.

public class CustomToolbar extends Toolbar {

    public CustomToolbar(Context context) {
        super(context);
    }

    public CustomToolbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        for(int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (child instanceof ActionMenuView) {
                child.setTop(0); // FIXME: Move child in vertically center to be exact
            }
        }
    }
}

In your layout:

<{replace here with package name}.CustomToolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorAccent"
        ... />
Автор: nshmura Размещён: 20.08.2016 05:20
Вопросы из категории :
32x32