首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android新手之旅(8) ListView的使用

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

作者头像
用户1075292
发布2018-01-23 14:49:38
6100
发布2018-01-23 14:49:38
举报
文章被收录于专栏:听雨堂听雨堂

  希望使用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
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 (二)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2011-07-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档