【问题标题】:How to set custom layout to item of menu?如何将自定义布局设置为菜单项?
【发布时间】:2023-03-26 22:38:02
【问题描述】:

在我的应用程序中,我想使用带有 iconpopUp 菜单,并编写以下代码。
但我想将我的 自定义布局 设置为 menu items,但不显示任何项目。
我的意思是,不显示菜单标题和图标!

我的 Java 代码:

public void onMoreMenu(View view) {
    showPopupWindow(view);
}

void showPopupWindow(View view) {
    PopupMenu popup = new PopupMenu(AuctionDetailPage.this, view);
    try {
        Field[] fields = popup.getClass().getDeclaredFields();
        for (Field field : fields) {
            if ("mPopup".equals(field.getName())) {
                field.setAccessible(true);
                Object menuPopupHelper = field.get(popup);
                Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
                Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class);
                setForceIcons.invoke(menuPopupHelper, true);
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    popup.getMenuInflater().inflate(R.menu.detail_menu, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        public boolean onMenuItemClick(MenuItem item) {
            Toast.makeText(getApplicationContext(), "You Clicked : " + item.getTitle(),  Toast.LENGTH_SHORT).show();
            return true;
        }
    });
    popup.show();
}

菜单代码:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <group android:checkableBehavior="none">

        <item
            android:id="@+id/nav_2"
            android:title=""
            app:actionLayout="@layout/item_test"
            app:showAsAction="always" />

    </group>

</menu>

自定义布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp">

    <ImageView
        android:id="@+id/itemNav_img"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerVertical="true"
        android:tint="#876"
        android:layout_toRightOf="@+id/itemNav_txt"
        android:src="@drawable/about" />

    <TextView
        android:id="@+id/itemNav_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textColor="#876"
        android:text="Test" />

</RelativeLayout>

如何修复它并将我的自定义布局设置为菜单项?

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 你想把它作为一个弹出菜单还是只是想在默认菜单上自定义布局?
  • @UmangBurman,我想将自定义布局设置为菜单并将其用于弹出菜单。你能帮帮我吗?
  • 是的,我也做过类似的事情……等一下……
  • @UmangBurman,谢谢我的兄弟。我会等你,请帮助我。我真的需要你的帮助。谢谢
  • 好的,你遇到了什么错误?

标签: java android android-menu


【解决方案1】:

好的,所以这可以通过以下步骤使用 ListPopupWindow(根据文档)完成:

第一步:创建模型类

public class Item {
    private String title;
    private int imageRes;

    public Item(String title, int imageRes) {
        this.title = title;
        this.imageRes = imageRes;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getImageRes() {
        return imageRes;
    }

    public void setImageRes(int imageRes) {
        this.imageRes = imageRes;
    }
}

第二步:创建适配器

public class ListPopupWindowAdapter extends BaseAdapter {
    LayoutInflater mLayoutInflater;
    List<Item> mItemList;

    public ListPopupWindowAdapter(Context context, List<Item> itemList) {
        mLayoutInflater = LayoutInflater.from(context);
        mItemList = itemList;
    }

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

    @Override
    public Item getItem(int i) {
        return mItemList.get(i);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.detail_menu, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvTitle.setText(getItem(position).getTitle());
        holder.ivImage.setImageResource(getItem(position).getImageRes());

        return convertView;
    }

    static class ViewHolder {
        TextView tvTitle;
        ImageView ivImage;

        ViewHolder(View view) {
            tvTitle = view.findViewById(R.id.text);
            ivImage = view.findViewById(R.id.image);
        }
    }
}

第三步:your_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp">

<ImageView
    android:id="@+id/itemNav_img"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_centerVertical="true"
    android:tint="#876"
    android:layout_toRightOf="@+id/itemNav_txt"
    android:src="@drawable/about" />

<TextView
    android:id="@+id/itemNav_txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:textColor="#876"
    android:text="Test" />

</RelativeLayout>

最后显示ListPopupWindow

private void showListPopupWindow(View anchor) {
    final ListPopupWindow popupWindow = new ListPopupWindow(this);

    List<Item> itemList = new ArrayList<>();
    itemList.add(new Item("A", R.mipmap.ic_launcher));
    itemList.add(new Item("B", R.mipmap.ic_launcher));
    itemList.add(new Item("C", R.mipmap.ic_launcher));

    ListAdapter adapter = new ListPopupWindowAdapter(this, itemList);
    popupWindow.setAnchorView(anchor);
    popupWindow.setAdapter(adapter);
    popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            popupWindow.dismiss();
        }
    });
    popupWindow.show();
}

就是这样,试试看,然后告诉我。

【讨论】:

  • 感谢我亲爱的兄弟的帮助。我使用您的代码并向我显示具有自定义布局的弹出菜单。但显示项目非常小,不显示标题和图像全文。为什么?
  • 我认为问题是 View anchor 。这个菜单宽度是菜单图像宽度!
  • 你能帮帮我吗?
  • 需要在popupwindow对象中定义宽度:popupWindow.width = ListPopupWindow.MATCH_PARENT或者固定宽度。
【解决方案2】:
【解决方案3】:
void showPopupWindow(View view)
    {
        PopupMenu popup = new PopupMenu(getBaseActivity(), view);
        try {
            Field[] fields = popup.getClass().getDeclaredFields();
            for (Field field : fields) {
                if ("mPopup".equals(field.getName()))
                {
                    field.setAccessible(true);
                    Object menuPopupHelper = field.get(popup);
                    Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
                    Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class);
                    setForceIcons.invoke(menuPopupHelper, true);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        popup.getMenuInflater().inflate(R.menu.job_menu, popup.getMenu());

        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
        {

            public boolean onMenuItemClick(MenuItem item)
            {
                int id = item.getItemId();

                if (id == R.id.postNewJob) {
                    Intent intent = new Intent(getBaseActivity(),CreateJobActivity.class);
                    getBaseActivity().startActivity(intent);
                    return true;
                }

                if (id == R.id.renewExpiredJob) {
                    Intent intent = new Intent(getBaseActivity(),RenewExpireJobListActivity.class);
                    getBaseActivity().startActivity(intent);
                    return true;
                }

                if (id == R.id.viewMyJobs)
                {
                    Intent intent = new Intent(getBaseActivity(), MyJobActivity.class);
                    getBaseActivity().startActivity(intent);
                    return true;
                }

                return false;

            }
        });
        popup.show();
    }

工作菜单:

<menu
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/postNewJob"
        android:title="Post New Job"
        android:icon="@drawable/icon_add_black"/>

    <item
        android:id="@+id/renewExpiredJob"
        android:title="Renew Expired Job"
        android:icon="@drawable/icon_expired_job"/>

    <item
        android:id="@+id/viewMyJobs"
        android:title="View my Job Posts"
        android:icon="@drawable/icon_jobs_tab_black"/>

</menu>

【讨论】:

  • 嘿,亲爱的,你看我的文字了吗?我想将自定义布局设置为菜单。我可以使用弹出菜单
猜你喜欢
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-19
相关资源
最近更新 更多