简体   繁体   English

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

[英]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.

相关问题 Android:如何在列表视图中为动态创建的项目设置侦听器 - Android: How to set listeners for dynamically created items in a list view 如何在android中的菜单项中设置字体真棒图标? - How to set the font awesome icons in menu items in android? 如何将列表视图项目的字体设置为Marathi - How to set Font of List View Items to Marathi android列表查看项 - android list view items 如何在Android中更改列表视图项的顺序 - How to change sequence of list View items in android 如何在Android中制作商品的列表视图? - How to make List view for items in Android? Android:SearchView:如何为建议列表中的每个项目设置单独的图标? - Android: SearchView: How set individual icons for every item in suggestion list? Android:具有基本适配器的自定义列表视图:如何在点击列表器中设置行项目? - Android: custom List view with base adapter: How to set on click listner for row items? 如何根据Android中的数据库值动态设置列表视图中所有项目的文本大小? - How to dynamically set text size of all items in a list view based on database value in Android? Android:如何为卡片视图阵列列表中的每个单个项目设置ID,并且可点击? - Android : How to set ID's for every single items in list of arrays of card view and be clickable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM