程序xml文件:
list_view.xml://主文件
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#ffc9c9c9"
android:dividerHeight="1px"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#ffc9c9c9"
android:dividerHeight="1px"/>
</LinearLayout>
item_custom.xml://定义想要的布局效果
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="58px"
android:gravity="center_vertical"
>
<ImageView android:id="@+id/index_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ImageView>
<TextView android:id="@+id/index_text"
android:layout_width="wrap_content"
android:layout_height="50px"
android:textSize="20sp"
android:layout_marginLeft="10px"
android:gravity="center"
android:textColor="#ff000000">
</TextView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="58px"
android:gravity="center_vertical"
>
<ImageView android:id="@+id/index_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ImageView>
<TextView android:id="@+id/index_text"
android:layout_width="wrap_content"
android:layout_height="50px"
android:textSize="20sp"
android:layout_marginLeft="10px"
android:gravity="center"
android:textColor="#ff000000">
</TextView>
</LinearLayout>
程序代码:
view plaincopy to clipboardprint?
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class CustomListview extends Activity {
private ListView mListView;
private List<String> items;
@Override
public void onCreate(Bundle savedInstanceStated) {
super.onCreate(savedInstanceStated);
setContentView(R.layout.list_view);
mListView = (ListView) findViewById(R.id.list_view);
fillArray();
mListView.setAdapter(new CustomListAdapter(this));
}
private void fillArray() {
items = new ArrayList<String>();
items.add("白天");
items.add("黑天");
items.add("多云");
items.add("微风");
items.add("晴天");
}
class CustomListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context mContext = null;
public CustomListAdapter(Context context) {
mContext = context;
mInflater = LayoutInflater.from(mContext);
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return items.get(arg0);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
public View getView(int position, View convertView,
android.view.ViewGroup parent) {
final ImageView indexImage;
final TextView indexText;
if (convertView == null) {
// 和item_custom.xml脚本关联
convertView = mInflater.inflate(R.layout.item_custom, null);
}
indexImage = (ImageView) convertView.findViewById(R.id.index_image);
indexText = (TextView) convertView.findViewById(R.id.index_text);
// 设置item中indexText的文本
indexText.setText(items.get(position).toString());
indexText.setTextColor(Color.RED);
// 设置item中ImageView的图片
indexImage.setBackgroundResource(R.drawable.icon);
return convertView;
}
}
}
本Blog纯属个人学习、工作需要,记录相关资料。请不要发表任何有人身攻击的言论,谢谢! www.zhipsoft.cn
posted on 2011-01-25 17:12
ZhipSoft 阅读(6046)
评论(0) 编辑 收藏 所属分类:
AndRoid