繁体   English   中英

如何在Android上为列表视图项目设置图标

[英]How to set icons for a list view items on 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函数上添加它吗?

正如@ckpatel所提到的,您可以制作一个自定义适配器,请参阅教程(首选),或通过编程方式为每个textview设置drawableRight ,就像设置textcolor一样。

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

试试这个代码的图标

TextView lbl = (TextView) convertView;
        lbl.setText(f.getTitle());
        lbl.setCompoundDrawablesWithIntrinsicBounds(f.getImage(), 0, 0, 0);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM