![](/img/trans.png)
[英]Android: How to set listeners for dynamically created items in a list view
[英]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.