如何在android上设置列表视图项的图标

时间:2014-09-15 13:07:04

标签: android

我想在我的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;
}
});

我应该在我的getView功能上添加它吗?

2 个答案:

答案 0 :(得分:1)

正如@ckpatel所提到的,您可以创建自定义适配器,查看this教程(首选)或以编程方式将 drawableRight 设置为每个textview,就像设置textcolor一样。

Drawable image =myContext.getResources().getDrawable( R.drawable.ic_launcher);
textView.setCompoundDrawablesWithIntrinsicBounds( null, null, image, null);

答案 1 :(得分:1)

尝试使用此代码作为图标

TextView lbl = (TextView) convertView;
        lbl.setText(f.getTitle());
        lbl.setCompoundDrawablesWithIntrinsicBounds(f.getImage(), 0, 0, 0);
相关问题