为什么我的下拉菜单不在cardview的点下?

时间:2018-03-10 14:59:02

标签: android menu cardview

Dropdown menu is not popped under the dots button on cardview

以下是我为此写的课程

我创建了自定义菜单,并在cardview工具栏上应用该菜单(即垂直3个点)我不知道问题出在哪里,下拉菜单的对齐方式已经改变,我现在无法修复它

产品适配器类具有以下所有功能:

public class ProdAdapter extends BaseAdapter {

    private PopupMenu mPopupMenu;

    Context context;
    ArrayList<Model> models;

    public ProdAdapter(Context context, ArrayList<Model> models) {
        this.context = context;
        this.models = models;
    }

    @Override
    public int getCount() {
        return models.size();
    }

    @Override
    public Object getItem(int i) {
        return models.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        if(view == null){
            view = View.inflate(context, R.layout.list_items,null);
        }

        ImageView images = (ImageView) view.findViewById(R.id.prodpic);
        TextView prodname = (TextView) view.findViewById(R.id.prodname);
        TextView prodbrand = (TextView) view.findViewById(R.id.prodbrand);
        TextView prodprice = (TextView) view.findViewById(R.id.prodprice);
        TextView prodstatus = (TextView) view.findViewById(R.id.status);
        Toolbar prodmenu = (Toolbar) view.findViewById(R.id.prodmenu);

        mPopupMenu = new PopupMenu(context, prodmenu);
        MenuInflater menuInflater = mPopupMenu.getMenuInflater();
        menuInflater.inflate(R.menu.product_menu, mPopupMenu.getMenu());
        //mPopupMenu.inflate(R.menu.product_menu);
        prodmenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupMenu.show();
            }
        });

        mPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                int id = menuItem.getItemId();
                if (id == R.id.action_edit) {
                    //Intent intent = new Intent(MainActivity.this,SettingsActivity.class);
                    //startActivity(intent);
                    return true;
                }
                if (id == R.id.action_delete) {
                    //Intent intent = new Intent(MainActivity.this,SettingsActivity.class);
                    //startActivity(intent);
                    return true;
                }
                return true;
            }
        });

        Model model = models.get(i);

        images.setImageResource(model.getProd_img());
        prodname.setText(model.getProd_name());
        prodbrand.setText(model.getProd_brand());
        prodprice.setText(model.getProd_price());
        prodstatus.setText(model.getProd_status());

        return view;
    }
}

prod_menu.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">
    <item
        android:id="@+id/action_edit"
        android:orderInCategory="100"
        android:title="Edit"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_delete"
        android:orderInCategory="100"
        android:title="Delete"
        app:showAsAction="never" />
</menu>

list_items.xml下面的卡片视图布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardUseCompatPadding="true"
    android:layout_marginBottom="16dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:paddingBottom="4dp">

        <ImageView
            android:id="@+id/prodpic"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="5dp"
            card_view:srcCompat="@mipmap/dd_logo" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/prodmenu"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/prodname"
            android:layout_marginRight="10dp"
            android:background="@drawable/dots" />

        <TextView
            android:id="@+id/prodname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_toEndOf="@+id/prodpic"
            android:layout_toLeftOf="@+id/prodmenu"
            android:layout_toRightOf="@+id/prodpic"
            android:layout_toStartOf="@+id/prodmenu"
            android:fontFamily="sans-serif-condensed"
            android:text="Product Name"
            android:textColor="#000"
            android:textStyle="bold"
            android:typeface="normal" />

        <TextView
            android:id="@+id/prodbrand"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/prodname"
            android:layout_alignStart="@+id/prodname"
            android:layout_below="@+id/prodname"
            android:layout_marginTop="10dp"
            android:layout_toLeftOf="@+id/prodmenu"
            android:layout_toStartOf="@+id/prodmenu"
            android:fontFamily="sans-serif-condensed"
            android:text="Product Brand"
            android:textColor="#000"
            android:typeface="normal" />

        <TextView
            android:id="@+id/prodprice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView4"
            android:layout_alignBottom="@+id/textView4"
            android:layout_toEndOf="@+id/textView4"
            android:layout_toRightOf="@+id/textView4"
            android:fontFamily="sans-serif-condensed"
            android:paddingLeft="5dp"
            android:text="Product Price"
            android:textColor="@color/colorAccent"
            android:typeface="normal" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/prodbrand"
            android:layout_alignStart="@+id/prodbrand"
            android:layout_below="@+id/prodbrand"
            android:layout_marginTop="10dp"
            android:fontFamily="sans-serif-condensed"
            android:text="PKR"
            android:textColor="@color/colorAccent"
            android:typeface="normal" />

        <TextView
            android:id="@+id/status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/prodmenu"
            android:layout_alignParentBottom="true"
            android:layout_alignRight="@+id/prodmenu"
            android:fontFamily="sans-serif-condensed"
            android:text="Status"
            android:textColor="@color/cardview_dark_background"
            android:typeface="normal" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

1 个答案:

答案 0 :(得分:1)

当前代码将菜单附加到listView的viewgroup root。您需要在列表项单击时调用您的弹出菜单,并将listItem视图作为锚点。

public void showMenu(View v) {
    PopupMenu popup = new PopupMenu(this, v);
    // This activity implements OnMenuItemClickListener
    popup.setOnMenuItemClickListener(this);
    popup.inflate(R.menu.actions);
    popup.show();
}

请参阅Creating a Popup Menu