在我的应用程序中,我想将 popUp 菜单与 icon 一起使用,并编写以下代码。
但是我想将我的自定义布局设置为menu
项目,但不显示任何项目。
我的意思是,不显示菜单标题和图标!
我的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>
如何解决它并将我的自定义布局设置为菜单项?
任何帮助将不胜感激。谢谢。
答案 0 :(得分:1)
好吧,因此可以通过以下步骤使用ListPopupWindow(根据文档)完成此操作:
第1步:创建模型类
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;
}
}
第2步:创建适配器
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
<?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();
}
就是这样,尝试一下,让我知道。
答案 1 :(得分:0)
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>
答案 2 :(得分:0)