为什么我的下拉菜单没有出现在 cardview 的点下?

萨尼亚·贾米尔

下拉菜单不会在 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>
克什蒂·马哈特

当前代码将菜单附加到 listView 的视图组根。您的弹出菜单需要在单击列表项时调用,以 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();
}

请参阅创建弹出菜单

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Bootstrap 4下拉菜单出现在其他元素后面

Appcompat CardView和Picasso没有圆角

Cardview阴影未出现在棒棒糖设备中?

如何在没有警告或ngModel的情况下使下拉菜单成角度?

我的CardView和TextInputLayout有问题

CardView没有显示在片段中

带有CardView的RecyclerView:什么都没有显示

为什么我的CardView不会出现在我的片段中?

下拉菜单未出现在特定按钮下方

为什么我的锚点无法在下拉菜单中导航?

如何在没有Java脚本的情况下重置primefaces commandButton的下拉菜单?

如何使Bootstrap 3下拉菜单对没有JS用户起作用

在没有Jquery的情况下将列表项转换为下拉菜单

下拉菜单出现在父菜单的下方

在没有grep的情况下返回出现在R列表中的元素的数量

带有CardView的RecyclerView出现性能问题

我想避免文件扩展名出现在Rshiny的下拉菜单中。

CMFCColorButton没有出现在MFC类向导下拉菜单中

在Android的Cardview中带有三个点的小部件的名称是什么?

为什么ImageView在CardView中褪色?

使子菜单出现在父菜单下(中央下拉菜单)

为什么Skype不会出现在“设置”>“隐私”下?

为什么Cardview在Appbar之上?

TextView 没有出现在 cardView 上

HorizontalScrollView 中有许多 CardView

为什么我的下拉菜单没有出现在悬停时?

带有 RecycelView 的 CardView 让我白屏

Android CardView 没有设置正确的颜色

如何在没有按钮的情况下使用 onclick 下拉菜单