为什么菜单项在工具栏上如此奇怪地对齐?
对于较小的工具栏,它的行为更加奇怪:
我使用以下代码:
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;
}
活动主.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>
样式.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>