我想创建一个如下所示的弹出菜单,
我这样使用actionLayout
属性
<?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">
<item
android:id="@+id/action_1"
android:title="@string/title"
app:showAsAction="always"
android:actionLayout="@layout/item_1" />
<item
android:id="@+id/action_2"
android:title="@string/title"
app:showAsAction="always"
android:actionLayout="@layout/item_2" />
<item
android:id="@+id/action_3"
android:title="@string/title"
app:showAsAction="always"
android:actionLayout="@layout/item_3" />
</menu>
我的item_1
布局将是这样,
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="100dp"
android:padding="5dp">
<View
android:id="@+id/view1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#fff9c4"
app:layout_constraintBottom_toBottomOf="@+id/imageTemp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageTemp" />
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/image"
android:padding="@dimen/dp8"
android:src="@drawable/ic_baseline_remove_red_eye_24px"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我已经使用此代码创建了PopupMenu
,
PopupMenu popupMenu = new PopupMenu(Objects.requireNonNull(fragment.getActivity()), v);
popupMenu.getMenuInflater().inflate(R.menu.menu_attachment, popupMenu.getMenu());
popupMenu.show();
但是不幸的是我无法实现我想要的。当前输出为