如何在Android中的arrayList中的文本旁边添加图像? [英] How do you add images beside text in an arrayList in Android?
本文介绍了如何在Android中的arrayList中的文本旁边添加图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void initDrawerList() {
// initialize drawer content
// need to determine selected item according to the currently selected sensor type
drawerItemList = new ArrayList();
drawerItemList.add(new DrawerItem("*1st text", R.drawable.my_sensz_normal, R.drawable.my_sensz_selected, true));
drawerItemList.add(new DrawerItem("*2nd text", R.drawable.friends_normal, R.drawable.friends_selected, false));
drawerItemList.add(new DrawerItem("*3rd text", R.drawable.friends_normal, R.drawable.friends_selected, false));
drawerAdapter = new DrawerAdapter(HomeActivity.this, drawerItemList);
drawerListView = (ListView) findViewById(R.id.drawer);
if (drawerListView != null)
drawerListView.setAdapter(drawerAdapter);
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}
我想用图像替换*,但是我不知道该怎么做.有人可以帮忙吗?谢谢!:)
I want to replace the * with an image, but I can't figure how to do so. Can someone help? Thanks! :)
推荐答案
首先,为自定义行创建布局文件 navigation_row.xml
at first you create a layout file navigation_row.xml
for custom row
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:orientation="horizontal"
android:gravity="center"
android:padding="10dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Icon"
android:src="@drawable/order_64" />
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Example application"
android:textColor="@color/whiteColor"
android:paddingLeft="10dp"
android:textSize="20sp" />
</LinearLayout>
在您的活动课中列出两个清单
in your activity class make two list
int [] icon = {R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
R.drawable.img5};
String [] drawerTitle ={"1st text",
"2 text",
"3 text",
"4 text",
"5 text"};
创建适配器类
public class NavigationAdapter extends ArrayAdapter <String> {
private final Context context;
private final String[] titles;
private final int[] icon;
public NavigationAdapter(Context context, int[] icon, String[] titles){
super(context, R.layout.navigation_row,titles);
this.context=context;
this.icon=icon;
this.titles=titles;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.navigation_row,parent,false);
ImageView imageView=(ImageView)rowView.findViewById(R.id.icon);
TextView textView = (TextView)rowView.findViewById(R.id.name);
imageView.setImageResource(icon[position]);
textView.setText(titles[position]);
return rowView;
}
}
现在将这个适配器对象设置为您的列表视图
and now set this adapter object to your listview
NavigationAdapter onAdapter=new NavigationAdapter(Navigation.this,icon,drawerTitle);
drawerListView.setAdapter(onAdapter);
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
这篇关于如何在Android中的arrayList中的文本旁边添加图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文