本文来自http://blog.csdn.net/hellogv/
Android入门第六篇之ListView (一) ,讲的是如何制作一个具有两行文本的 自定义控件 ,作为ListView的Item的使用方法。这篇接下来也是围绕ListView和Item,更加深入地介绍它们的用法。
首先,先来看看本文代码运行的结果,本文的Item比上一篇中的Item多出左边的图标:
main.xml的源代码,跟上一篇的一样,这里就不作解释了,直接贴出item.xml的代码,就是它实现ImageItem的UI:
-
xml version="1.0" encoding="utf-8"?>
-
<RelativeLayout
-
android:layout_width="fill_parent"
-
xmlns: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的使用方法如下:
-
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 = inflater.inflate(R.layout.item, null);
-
-
-
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;
-
}
-
}
-
-
}
阅读(544) | 评论(0) | 转发(0) |