我有一个带有ContextMenu的Listview,但是当我为ContextMenu设置setIcon时看起来它不起作用
public void onCreateContextMenu(ContextMenu menu , View v,
ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.context_menu_favorite)
.setIcon(android.R.drawable.btn_star);
}
答案 0 :(得分:39)
Context menus不支持图标。
注意:上下文菜单项没有 支持图标或快捷键。
答案 1 :(得分:4)
此库允许您使用标准XML菜单创建带有图标的上下文菜单(实现为AlertDialog
)。
答案 2 :(得分:4)
我是这样做的:
参考屏幕截图:
<强> menu_patient_language.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activities.PatientSelectionActivity">
<item
android:id="@+id/menuEnglish"
android:icon="@drawable/language_english"
android:title="@string/english" />
<item
android:id="@+id/menuFrench"
android:icon="@drawable/language_french"
android:title="@string/french" />
</menu>
<强> style.xml:强>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:popupMenuStyle">@style/popupMenuStyle</item>
</style>
<!--- Language selection popup -->
<style name="popupMenuStyle" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:textColor">@color/colorPrimary</item>
<item name="android:itemBackground">@android:color/white</item>
</style>
private void showPopup(View v) {
Context wrapper = new ContextThemeWrapper(this, R.style.popupMenuStyle);
PopupMenu mypopupmenu = new PopupMenu(wrapper, v);
setForceShowIcon(mypopupmenu);
MenuInflater inflater = mypopupmenu.getMenuInflater();
inflater.inflate(R.menu.menu_patient_language, mypopupmenu.getMenu());
mypopupmenu.show();
// mypopupmenu.getMenu().getItem(0).setIcon(getResources().getDrawable(R.mipmap.ic_launcher));
mypopupmenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
txtPreferredLanguage.setText(item.getTitle().toString());
switch (item.getItemId()) {
case R.id.menuEnglish:
// Your code goes here
break;
case R.id.menuFrench:
// Your code goes here
break;
}
return false;
}
});
}
private void setForceShowIcon(PopupMenu popupMenu) {
try {
Field[] mFields = popupMenu.getClass().getDeclaredFields();
for (Field field : mFields) {
if ("mPopup".equals(field.getName())) {
field.setAccessible(true);
Object menuPopupHelper = field.get(popupMenu);
Class<?> popupHelper = Class.forName(menuPopupHelper.getClass().getName());
Method mMethods = popupHelper.getMethod("setForceShowIcon", boolean.class);
mMethods.invoke(menuPopupHelper, true);
break;
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
希望这会对你有所帮助。
完成强>
答案 3 :(得分:2)
虽然Android API不允许上下文菜单中的图标,但您可以看到Android正在使用它们的许多地方。长按主屏幕就是一个很好的例子。
我花时间浏览了Launcher和AnyCut源代码,发现Google正在使用自己的自定义类,扩展BaseAdapter以及自己的自定义布局。
我能够几乎完全复制他们的类和布局,并在我自己的应用程序中使用它来完成。如果要搜索它,该类称为AddAdapter.java。
享受!
答案 4 :(得分:0)
检查一下......
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Settings");
menu.setHeaderIcon(R.drawable.ic_settings);
menu.add(0, v.getId(), 0, "Action 1");
menu.add(0, v.getId(), 0, "Action 2");
menu.add(0, v.getId(), 0, "Action 3");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Action 1") {
Toast.makeText(this, "Action 1 invoked", Toast.LENGTH_SHORT).show();
} else if (item.getTitle() == "Action 2") {
Toast.makeText(this, "Action 2 invoked", Toast.LENGTH_SHORT).show();
} else if (item.getTitle() == "Action 3") {
Toast.makeText(this, "Action 3 invoked", Toast.LENGTH_SHORT).show();
} else {
return false;
}
return true;
}
答案 5 :(得分:0)
虽然API不支持上下文菜单中的图标,但我们总是可以通过使用我们自己的视图来扩展Dialog来伪造它,看起来像上下文菜单。
完全复制粘贴以下文件将完成这项工作:
<强> MainActivity.java 强>
public class MainActivity extends Activity {
List<ContextMenuItem> contextMenuItems;
Dialog customDialog;
LayoutInflater inflater;
View child;
ListView listView;
ContextMenuAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = inflater.inflate(R.layout.listview_context_menu, null);
listView = (ListView) child.findViewById(R.id.listView_context_menu);
contextMenuItems = new ArrayList<ContextMenuItem>();
contextMenuItems.add(new ContextMenuItem(getResources().getDrawable(
R.drawable.ic_launcher), "Facebook"));
contextMenuItems.add(new ContextMenuItem(getResources().getDrawable(
R.drawable.ic_launcher), "Scanner"));
adapter = new ContextMenuAdapter(this,
contextMenuItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
customDialog.dismiss();
if (position == 0)
Toast.makeText(MainActivity.this, "00", Toast.LENGTH_SHORT)
.show();
if (position == 1)
Toast.makeText(MainActivity.this, "11", Toast.LENGTH_SHORT)
.show();
}
});
customDialog = new Dialog(this);
customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
customDialog.setContentView(child);
customDialog.show();
}
}
<强> ContextMenuItem.java 强>
public class ContextMenuItem {
Drawable drawable;
String text;
public ContextMenuItem(Drawable drawable, String text) {
super();
this.drawable = drawable;
this.text = text;
}
public Drawable getDrawable() {
return drawable;
}
public void setDrawable(Drawable drawable) {
this.drawable = drawable;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
<强> ContextMenuAdapter.java 强>
public class ContextMenuAdapter extends BaseAdapter {
Context context;
List<ContextMenuItem> listContextMenuItems;
LayoutInflater inflater;
public ContextMenuAdapter(Context context,
List<ContextMenuItem> listContextMenuItems) {
super();
this.context = context;
this.listContextMenuItems = listContextMenuItems;
}
static class ViewHolder {
protected ImageView imageView;
protected TextView textView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.context_menu_item, parent,
false);
viewHolder.imageView = (ImageView) convertView
.findViewById(R.id.imageView_menu);
viewHolder.textView = (TextView) convertView
.findViewById(R.id.textView_menu);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.imageView.setImageDrawable(listContextMenuItems
.get(position).getDrawable());
viewHolder.textView.setText(listContextMenuItems.get(position)
.getText());
return convertView;
}
@Override
public int getCount() {
return listContextMenuItems.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
<强> context_menu_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="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp" >
<ImageView
android:id="@+id/imageView_menu"
android:layout_width="70dp"
android:layout_height="70dp"
android:scaleType="fitXY" />
<TextView
android:id="@+id/textView_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/imageView_menu" />
</RelativeLayout>
<强> listview_context_menu.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView_context_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/view" />