欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

安卓(Android)ListView 显示图片文字

程序员文章站 2024-03-03 21:05:46
一.代码实现 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>