Why are menu items aligned so strangely on the ActionMenuView inside the toolbar?

418 views Asked by At

Why are menu items aligned so strangely on the toolbar?

enter image description here

It behaves even more strangely with smaller toolbar:

enter image description here

I use the following code:

MainActivity.java:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //getMenuInflater().inflate(R.menu.menu_main_right, menu);

    Toolbar toolbar = findViewById(R.id.toolbar);
    ActionMenuView toolbarLeft = toolbar.findViewById(R.id.toolbarLeft);
    getMenuInflater().inflate(R.menu.menu_main_left, toolbarLeft.getMenu());

    return true;
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ActionBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.v7.widget.ActionMenuView
            android:id="@+id/toolbarLeft"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:gravity="center_vertical|start"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.v7.widget.Toolbar>

</android.support.constraint.ConstraintLayout>

styles.xml:

<resources>

    <style name="ActionBar">
        <item name="android:background">@drawable/toolbar</item>
        <item name="actionMenuTextColor">@color/colorToolbarText</item>
    </style>

</resources>

menu_main_left.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">

    <item
        android:id="@+id/actionAdd"
        android:title="Add"
        app:showAsAction="always"/>

    <item
        android:id="@+id/actionDetails"
        android:title="Details"
        app:showAsAction="always"/>

    <item
        android:id="@+id/actionLegend"
        android:title="Legend"
        app:showAsAction="always"/>

    <item
        android:id="@+id/actionSettings"
        android:title="Settings"
        app:showAsAction="always"/>

</menu>
0

There are 0 answers