上一篇关于Android中ListView的介绍讲的是如何制作一个具有两行文本的自定义控件,作为ListView的Item的使用方法。本文接下来也是围绕ListView和Item,更加深入地介绍它们的用法。
首先,先来看看本文代码运行的结果,本文的Item比上一篇中的Item多出左边的图标:
main.xml的源代码,跟上一篇的一样,这里就不作解释了,直接贴出item.xml的代码,就是它实现ImageItem的UI:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<? 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 > |
解释一下 item.xml的代码:这里使用了RelativeLayout布局,控件的关键的属性是:
itemTitle的属性 android:layout_toRightOf="@+id/itemImage" ,itemTitle在itemImage的右边;
itemText的属性 android:layout_toRightOf="@+id/itemImage",ItemText在itemImage的右边, android:layout_below="@+id/itemTitle", itemText 在 itemTitle的下面。
最后,贴出JAVA的源代码,代码中的重点是LayoutInflate的用法。LayoutInflate的使用方法如下:
LayoutInflater的作用是,把一个View的对象与XML布局文件关联并实例化。
View的对象实例化之后,可以通过findViewById()查找布局文件中的指定Id的组件。
Java代码如下:
- package com.testListView;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- 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 testListView 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
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.setTitle("BaseAdapter for ListView");
- listView=(ListView)this.findViewById(R.id.listView1);
- 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) testListView.this
- .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;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- if (convertView == null)
- return itemViews[position];
- return convertView;
- }
- }
- }