在弹出菜单项中包含布局

Include layout inside popup menu items

我尝试在我的服务中为某些功能添加弹出菜单。我想添加一些水平图标。我尝试将它们添加到布局中,然后将该布局包含在菜单项中。但是什么都没有。

<?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/menu_id"
        android:title="TEST"
        app:showAsAction="always"
        app:actionLayout="@layout/menu_navigation"/>

    <item android:id="@+id/downloads"
        android:title="@string/downloads"
        android:icon="@drawable/ic_download"
        app:showAsAction="always|withText"/>

    <item android:id="@+id/settings"
        android:title="@string/settings"
        android:icon="@drawable/ic_settings"
        app:showAsAction="always|withText" />
</menu>

尝试使用 Popup Window 使用自定义布局,这样您就可以添加图标

 LayoutInflater layoutInflater = (LayoutInflater) 
 MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View customView = layoutInflater.inflate(R.layout.pop_window_layout,null);


    Button  but1 = (Button)customView.findViewById(R.id.popwinitem1);
    Button  but2 = (Button)customView.findViewById(R.id.popwinitem2);
    Button  but3 = (Button)customView.findViewById(R.id.popwinitem3);

    //instantiate popup window
    final PopupWindow popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    //display the popup window below whatever widget you want
    popupWindow.showAsDropDown(text1);
    //popupWindow.showAtLocation(holder.text1, Gravity.RIGHT, 0, 0);

    //close the popup window on button click


    but1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           // yourfunction();
            popupWindow.dismiss();
        }
    });
    but2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           // yourfunction();
            popupWindow.dismiss();
        }
    });
    but3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            popupWindow.dismiss();
        }
    });

pop_window_layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/colorWhite"
android:padding="10dp">

<Button
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:id="@+id/popwinitem1"
    android:padding="5dp"
    android:textSize="15sp"
    android:text="@string/save"
    android:textColor="@color/colorBlack"/>

<Button
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:id="@+id/popwinitem2"
    android:text="@string/delete"
    android:textSize="15sp"
    android:padding="5dp"
    android:textColor="@color/colorBlack"/>

<Button
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:id="@+id/popwinitem3"
    android:text="@string/cancel"
    android:padding="5dp"
    android:textSize="15sp"
    android:textColor="@color/colorBlack"/>