亲宝软件园·资讯

展开

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

人气:0
想了解安卓(Android)ListView 显示图片文字的相关内容吗,在本文为您仔细讲解Android ListView 用法的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,ListView用法,Android,ListView样式,Android,ListView布局,下面大家一起来学习吧。

一.代码实现

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>

加载全部内容

相关教程
猜你喜欢
用户评论