如何为 Android 的 ListView 中的菜单项创建图标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/288044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create icons for menu items in Android's ListView?
提问by Michael Herold
I am using a ListView
to display the main screen of my application.
The main screen is essentially a menu
to get into the different sections of application. Currently, I have the ListView
whose contents are added programmatically in the onCreate
method.
我正在使用 aListView
来显示我的应用程序的主屏幕。
主屏幕本质上是menu
进入应用程序的不同部分。目前,我ListView
在onCreate
方法中以编程方式添加了其内容。
Here is the code snippet that does this:
这是执行此操作的代码片段:
String[] mainItems = {
"Inbox", "Projects", "Contexts", "Next Actions"
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, mainItems));
registerForContextMenu(getListView());
}
So the menu is essentially just a bunch of nodes with the text contained in the mainItems array. I know that I can create an XML layout (i.e. R.layout.mainMenu_item
) that has an ImageView and TextView in it, but I am unsure how to set the ImageView's icon. I have seen that there is a setImageResouce(int resId) method, but the way to use this when generating with an ArrayAdapter is eluding me. Is there a better way to do this?
所以菜单本质上只是一堆节点,文本包含在 mainItems 数组中。我知道我可以创建一个R.layout.mainMenu_item
包含 ImageView 和 TextView的 XML 布局(即),但我不确定如何设置 ImageView 的图标。我已经看到有一个 setImageResouce(int resId) 方法,但是在使用 ArrayAdapter 生成时使用它的方法我不知道。有一个更好的方法吗?
采纳答案by jasonhudgins
What I typically do for a ListView is to implement my own Adapter by extending the handy BaseAdapter class. One of the abstract methods you'll implement will be getView() as the previous poster mentioned. From there you can inflate a layout containing an ImageView, get a reference to it using findViewById, and set the image to whatever drawable you've added into your resources.
我通常为 ListView 做的是通过扩展方便的 BaseAdapter 类来实现我自己的适配器。您将实现的抽象方法之一将是 getView(),如上一个海报所述。从那里您可以扩充包含 ImageView 的布局,使用 findViewById 获取对它的引用,并将图像设置为您添加到资源中的任何可绘制对象。
public View getView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.menu_row, null);
ImageView icon = (ImageView) row.findViewById(R.id.icon);
icon.setImageResource(..your drawable's id...);
return view;
}
回答by Feet
From the google docs for ArrayAdapter.
来自 ArrayAdapter 的谷歌文档。
To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.
要使用除 TextViews 之外的其他内容进行数组显示,例如 ImageViews,或者让除 toString() 结果之外的一些数据填充视图,请覆盖 getView(int, View, ViewGroup) 以返回您想要的视图类型。