收录日期:2020/06/02 04:08:42 时间:2010-03-04 17:37:50 标签:android,icons,android-context

I have a Listview with a ContextMenu, but when I setIcon for ContextMenu look like it doesn't work

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);    	
}

Context menus do not support icons.

Note: Context menu items do not support icons or shortcut keys.

This library allows you to have a context menu (implemented as AlertDialog) with icons using a standard XML menu.

https://code.google.com/p/android-icon-context-menu/

While the Android API does not allow for Icons in Context menus you can see many places where Android is using them. Long pressing your home screen is one good example.

I took the time to dig through the Launcher and AnyCut source and found that Google is using their own custom class that extends a BaseAdapter along with their own custom layout.

I was able to copy their class and layout almost exactly and use it in my own app to accomplish. The class if you want to search for it is called AddAdapter.java.

Enjoy!

check this one....

@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;
 }

While the API doesn't support icons in Context Menu, but we can always fake it by inflating a Dialog with our own view that looks like context menu.

Copy-pasting the following files exactly will do the job:

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" />