Android新手之旅(8) ListView的使用

  希望使用ListView来展示信息,每行一个图标,右侧是文字,分为两行布局。经过尝试,这样可以实现:

1、Layout下新建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="2dip"  
         android:paddingLeft="12dip"> 
         <ImageView  
               android:layout_width="wrap_content"  
               android:id="@+id/itemImage" android:layout_height="fill_parent"
               android:paddingTop="2dip" >  
         </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="16dip"
               android:textColor="#000"> 
         </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"
               android:textSize="10dip"
               android:textColor="#000"> 
         </TextView> 
</RelativeLayout>

2、主页面中Listview的样式

<ListView android:layout_marginTop="20px"  android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/MyListView"></ListView>

3、通过以下核心函数应用样式

private View makeItemView(String strTitle, String strText, int resId) {
    LayoutInflater inflater = (LayoutInflater) act
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // 使用View的对象itemView与R.layout.item关联
    View itemView = inflater.inflate(R.layout.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;
}

4、主程序中的使用,把一系列数组传递进去

listView=(ListView)this.findViewById(R.id.MyListView);
listView.setAdapter(new ListViewAdapterImageText(this, titles,texts,resIds));

5、效果

image

6、ListView的单击处理

import android.widget.AdapterView.OnItemClickListener;
import android.view.View; 

        //添加点击  
        listView.setOnItemClickListener(new OnItemClickListener() {  
            @Override 
            public void onItemClick(AdapterView<?> arg0,View arg1, int arg2,  
                    long arg3) {  
                setTitle("点击第"+arg2+"个项目");  
            }  
        });

参考:Android ListView常用用法 

参考:Android入门第七篇之ListView (二)

posted @   jetz  阅读(4184)  评论(1编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
历史上的今天:
2010-07-24 异步方式访问网页
2008-07-24 VB显示真彩图标
2007-07-24 如何清除应用程序承载 WebBrowser 控件时缓存
2005-07-24 最近读的文章
点击右上角即可分享
微信分享提示