安卓(Android)ListView 显示图片文字
一.代码实现
1. “Activity_11\src\yan\activity_11\MainActivity.java”
packageyan.activity_11; importandroid.os.Bundle; importandroid.app.Activity; importandroid.content.Context; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.BaseAdapter; importandroid.widget.ImageView; importandroid.widget.ListView; importandroid.widget.TextView; publicclassMainActivityextendsActivity{ ListViewlistView; String[]titles={"标题1","标题2","标题3","标题4"}; String[]texts={"文本内容A","文本内容B","文本内容C","文本内容D"}; int[]resIds={R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon}; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); this.setTitle("BaseAdapterforListView"); listView=(ListView)this.findViewById(R.id.MyListView); listView.setAdapter(newListViewAdapter(titles,texts,resIds)); } publicclassListViewAdapterextendsBaseAdapter{ View[]itemViews; publicListViewAdapter(String[]itemTitles,String[]itemTexts, int[]itemImageRes){ itemViews=newView[itemTitles.length]; for(inti=0;i<itemViews.length;++i){ itemViews[i]=makeItemView(itemTitles[i],itemTexts[i], itemImageRes[i]); } } publicintgetCount(){ returnitemViews.length; } publicViewgetItem(intposition){ returnitemViews[position]; } publiclonggetItemId(intposition){ returnposition; } privateViewmakeItemView(StringstrTitle,StringstrText,intresId){ LayoutInflaterinflater=(LayoutInflater)MainActivity.this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); //使用View的对象itemView与R.layout.item关联 ViewitemView=inflater.inflate(R.layout.listview_item,null); //通过findViewById()方法实例R.layout.item内各组件 TextViewtitle=(TextView)itemView.findViewById(R.id.itemTitle); title.setText(strTitle); TextViewtext=(TextView)itemView.findViewById(R.id.itemText); text.setText(strText); ImageViewimage=(ImageView)itemView.findViewById(R.id.itemImage); image.setImageResource(resId); returnitemView; } publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ if(convertView==null) returnitemViews[position]; returnconvertView; } } }
2.packageyan.activity_11;
importandroid.os.Bundle; importandroid.app.Activity; importandroid.content.Context; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.BaseAdapter; importandroid.widget.ImageView; importandroid.widget.ListView; importandroid.widget.TextView; publicclassMainActivityextendsActivity{ ListViewlistView; String[]titles={"标题1","标题2","标题3","标题4"}; String[]texts={"文本内容A","文本内容B","文本内容C","文本内容D"}; int[]resIds={R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon}; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); this.setTitle("BaseAdapterforListView"); listView=(ListView)this.findViewById(R.id.MyListView); listView.setAdapter(newListViewAdapter(titles,texts,resIds)); } publicclassListViewAdapterextendsBaseAdapter{ View[]itemViews; publicListViewAdapter(String[]itemTitles,String[]itemTexts, int[]itemImageRes){ itemViews=newView[itemTitles.length]; for(inti=0;i<itemViews.length;++i){ itemViews[i]=makeItemView(itemTitles[i],itemTexts[i], itemImageRes[i]); } } publicintgetCount(){ returnitemViews.length; } publicViewgetItem(intposition){ returnitemViews[position]; } publiclonggetItemId(intposition){ returnposition; } privateViewmakeItemView(StringstrTitle,StringstrText,intresId){ LayoutInflaterinflater=(LayoutInflater)MainActivity.this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); //使用View的对象itemView与R.layout.item关联 ViewitemView=inflater.inflate(R.layout.listview_item,null); //通过findViewById()方法实例R.layout.item内各组件 TextViewtitle=(TextView)itemView.findViewById(R.id.itemTitle); title.setText(strTitle); TextViewtext=(TextView)itemView.findViewById(R.id.itemText); text.setText(strText); ImageViewimage=(ImageView)itemView.findViewById(R.id.itemImage); image.setImageResource(resId); returnitemView; } publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ if(convertView==null) returnitemViews[position]; returnconvertView; } } }
二.“Activity_11\res\layout\main.xml”
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <ListViewandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/MyListView"> </ListView> </LinearLayout> <?xmlversion="1.0"encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <ListViewandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/MyListView"> </ListView> </LinearLayout>
三.“Activity_11\res\layout\listview_item.xml”
<?xmlversion="1.0"encoding="utf-8"?> <RelativeLayout android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:paddingBottom="4dip" android:paddingLeft="12dip"> <ImageView android:layout_width="wrap_content" android:id="@+id/itemImage" android:layout_height="fill_parent"> </ImageView> <TextView android:text="TextView01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/itemTitle" android:layout_toRightOf="@+id/itemImage" android:textSize="20dip"> </TextView> <TextView android:text="TextView02" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/itemText" android:layout_toRightOf="@+id/itemImage" android:layout_below="@+id/itemTitle"> </TextView> </RelativeLayout>
<?xmlversion="1.0"encoding="utf-8"?> <RelativeLayout android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:paddingBottom="4dip" android:paddingLeft="12dip"> <ImageView android:layout_width="wrap_content" android:id="@+id/itemImage" android:layout_height="fill_parent"> </ImageView> <TextView android:text="TextView01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/itemTitle" android:layout_toRightOf="@+id/itemImage" android:textSize="20dip"> </TextView> <TextView android:text="TextView02" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/itemText" android:layout_toRightOf="@+id/itemImage" android:layout_below="@+id/itemTitle"> </TextView> </RelativeLayout>