欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2021-01-18 14:44:47|栏目:Android代码|点击:

一.代码实现

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中炫酷的ViewPagerIndicator(不仅仿MIUI)

栏    目:Android代码

下一篇:浅谈android nexus私服的使用

本文标题:安卓(Android)ListView 显示图片文字

本文地址:http://www.codeinn.net/misctech/47025.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有