gpt4 book ai didi

java - 如何将自定义布局设置为菜单项?

转载 作者:行者123 更新时间:2023-11-29 23:38:44 26 4
gpt4 key购买 nike

在我的应用程序中,我想使用带有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>

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

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

最佳答案

好的,这可以通过以下步骤使用 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);
}
}
}

第三步:你的项目.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();
}

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

关于java - 如何将自定义布局设置为菜单项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52038877/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com