[英]How to set custom layout to item of menu?
In my application i want use popUp menu with icon and i write below codes.在我的应用程序中,我想使用带有图标的弹出菜单,并编写以下代码。
But i want set my custom layout to menu
items , but not show any item.但我想将我的自定义布局设置为menu
项,但不显示任何项目。
What I mean is , Not show menu title and icon!我的意思是,不显示菜单标题和图标!
My Java code :我的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 code:菜单代码:
<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>
Custom layout codes:自定义布局代码:
<?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>
How can I fix it and set my custom layout to menu items?如何修复它并将我的自定义布局设置为菜单项?
Any help will be appreciated.任何帮助将不胜感激。 Thanks.谢谢。
Try these tutorials may be your problem get resolved.尝试这些教程可能会解决您的问题。
https://stablekernel.com/using-custom-views-as-menu-items/ https://stablekernel.com/using-custom-views-as-menu-items/
https://medium.com/@prakharsrivastava_219/creating-custom-menu-in-android-part-1-3-menu-design-b51a13a7d018 https://medium.com/@prakharsrivastava_219/creating-custom-menu-in-android-part-1-3-menu-design-b51a13a7d018
Okay, so this can be done using ListPopupWindow(as per the docs) from the following steps:好的,所以这可以通过以下步骤使用 ListPopupWindow(根据文档)来完成:
Step1: Create a Model Class Step1:创建模型类
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;
}
}
Step2: Create an Adapter Step2:创建适配器
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);
}
}
}
Step3: your_item.xml第三步: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>
Finally, show ListPopupWindow
最后,显示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();
}
That's it, Try it and let me know.就是这样,试试吧,让我知道。
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();
}
job_menu:工作菜单:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.