如何显示图标右键菜单中的android? [英] How to show icons in context menu in android?

查看:249
本文介绍了如何显示图标右键菜单中的android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,在我的应用程序,当我点击它应该显示上下文菜单,但我知道,我们不能显示在上下文菜单中的图标。我看到有在他们的上下文菜单图标几个应用程序,如图中的链接:

I have a button on my app, when I click that it should display context Menu, But I know we can not show icons in context Menu. I have seen few Apps having icons in their context menu as shown in the link:

http://pavansdroidapps.blogspot.com/2011/06/context- menu.html

推荐答案

虽然API不支持在上下文菜单的图标,但是我们总是可以通过充气的对话与我们自己的观点,看起来像上下文菜单中捏造事实。

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

这篇关于如何显示图标右键菜单中的android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆