带有背景色的菜单项

时间:2019-04-25 08:53:24

标签: android

我想创建一个如下所示的弹出菜单,

enter image description here

我这样使用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();

但是不幸的是我无法实现我想要的。当前输出为

enter image description here

0 个答案:

没有答案