安卓(Android)ListView 显示图片文字
程序员文章站
2024-03-05 09:17:24
一.代码实现
1. “activity_11\src\yan\activity_11\mainactivity.java”
package ya...
一.代码实现
1. “activity_11\src\yan\activity_11\mainactivity.java”
package yan.activity_11; import android.os.bundle; import android.app.activity; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.listview; import android.widget.textview; public class mainactivity extends activity { listview listview; 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 protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); this.settitle("baseadapter for listview"); listview=(listview)this.findviewbyid(r.id.mylistview); listview.setadapter(new listviewadapter(titles,texts,resids)); } public class listviewadapter extends baseadapter{ view [] itemviews; public listviewadapter(string [] itemtitles, string [] itemtexts, int [] itemimageres){ itemviews = new view[itemtitles.length]; for (int i=0; i<itemviews.length; ++i){ itemviews[i] = makeitemview(itemtitles[i], itemtexts[i], itemimageres[i]); } } public int getcount() { return itemviews.length; } public view getitem(int position) { return itemviews[position]; } public long getitemid(int position) { return position; } private view makeitemview(string strtitle, string strtext, int resid) { layoutinflater inflater = (layoutinflater)mainactivity.this .getsystemservice(context.layout_inflater_service); // 使用view的对象itemview与r.layout.item关联 view itemview = inflater.inflate(r.layout.listview_item, null); // 通过findviewbyid()方法实例r.layout.item内各组件 textview title = (textview)itemview.findviewbyid(r.id.itemtitle); title.settext(strtitle); textview text = (textview)itemview.findviewbyid(r.id.itemtext); text.settext(strtext); imageview image = (imageview)itemview.findviewbyid(r.id.itemimage); image.setimageresource(resid); return itemview; } public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) return itemviews[position]; return convertview; } } }
2.package yan.activity_11;
import android.os.bundle; import android.app.activity; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.listview; import android.widget.textview; public class mainactivity extends activity { listview listview; 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 protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); this.settitle("baseadapter for listview"); listview=(listview)this.findviewbyid(r.id.mylistview); listview.setadapter(new listviewadapter(titles,texts,resids)); } public class listviewadapter extends baseadapter{ view [] itemviews; public listviewadapter(string [] itemtitles, string [] itemtexts, int [] itemimageres){ itemviews = new view[itemtitles.length]; for (int i=0; i<itemviews.length; ++i){ itemviews[i] = makeitemview(itemtitles[i], itemtexts[i], itemimageres[i]); } } public int getcount() { return itemviews.length; } public view getitem(int position) { return itemviews[position]; } public long getitemid(int position) { return position; } private view makeitemview(string strtitle, string strtext, int resid) { layoutinflater inflater = (layoutinflater)mainactivity.this .getsystemservice(context.layout_inflater_service); // 使用view的对象itemview与r.layout.item关联 view itemview = inflater.inflate(r.layout.listview_item, null); // 通过findviewbyid()方法实例r.layout.item内各组件 textview title = (textview)itemview.findviewbyid(r.id.itemtitle); title.settext(strtitle); textview text = (textview)itemview.findviewbyid(r.id.itemtext); text.settext(strtext); imageview image = (imageview)itemview.findviewbyid(r.id.itemimage); image.setimageresource(resid); return itemview; } public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) return itemviews[position]; return convertview; } } }
二.“activity_11\res\layout\main.xml”
<?xml version="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"> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/mylistview"> </listview> </linearlayout> <?xml version="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"> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/mylistview"> </listview> </linearlayout>
三.“activity_11\res\layout\listview_item.xml”
<?xml version="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>
<?xml version="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>
推荐阅读
-
安卓(Android)ListView 显示图片文字
-
安卓(Android)ListView 显示图片文字
-
Android 开发 使用WebUploader解决安卓微信浏览器上传图片中遇到的bug
-
Android 开发 使用WebUploader解决安卓微信浏览器上传图片中遇到的bug
-
Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右边)
-
Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右边)
-
安卓PopupWindow+ListView实现登录账号选择下拉框 博客分类: android开发 androidPopupWindow下拉框
-
android开发之TabHost标签控件的图片和文字不能同时显示的解决方案
-
安卓实现高亮显示文字中的某段文字
-
Android DrawableTextView图片文字居中显示实例