[英]How to set icons for a list view items on android
I want to add some icons for every items in my list view in my android application. 我想在Android应用程序的列表视图中为每个项目添加一些图标。
mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, android.R.id.text1,
new String[] {
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
getString(R.string.title_section4),
getString(R.string.title_section5),
getString(R.string.title_section6)}) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
textView.setTextColor(getResources().getColor(R.color.dark_grey));
textView.setTypeface(Typeface.DEFAULT_BOLD);
return textView;
}
});
Should I add it on my getView
function? 我应该在
getView
函数上添加它吗?
As @ckpatel mentioned you can make a custom adapter see this tutorial(preferred) or set drawableRight programatically to each of the textview just like you set the textcolor. 正如@ckpatel所提到的,您可以制作一个自定义适配器,请参阅本教程(首选),或通过编程方式为每个textview设置drawableRight ,就像设置textcolor一样。
Drawable image =myContext.getResources().getDrawable( R.drawable.ic_launcher);
textView.setCompoundDrawablesWithIntrinsicBounds( null, null, image, null);
try this code for icon 试试这个代码的图标
TextView lbl = (TextView) convertView;
lbl.setText(f.getTitle());
lbl.setCompoundDrawablesWithIntrinsicBounds(f.getImage(), 0, 0, 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.