热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

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

本文主要介绍Android中重要组件ListView,在编程中经常会用到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

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

二.“Activity_11\res\layout\main.xml”

<&#63;xml version="1.0" encoding="utf-8"&#63;>  
  
      
      
      
 

<&#63;xml version="1.0" encoding="utf-8"&#63;> 
 
     
     
     
 

三.“Activity_11\res\layout\listview_item.xml”

<&#63;xml version="1.0" encoding="utf-8"&#63;>   
   
        
        
        
        
        
        
 
<&#63;xml version="1.0" encoding="utf-8"&#63;>  
  
       
       
       
       
       
       

推荐阅读
author-avatar
zulaka_208
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有