还记得那个BeautyActivity吗?它对应的layout的xml里定义了一个ListView。要想在ListView里面加入item,只能通过代码(不能直接在xml里):
- String[] items = {"Item1", "Item2", "Item3"};
- int itemResId = android.R.layout.simple_list_item_1; // TextView
-
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(
- this, itemResId, items);
- ListView lv = (ListView)findViewById(R.id.my_list_view);
- lv.setAdapter(adapter);
android.R.layout.simple_list_item_1就是TextView的id。通过一个ArrayAdapter,ListView设置了自己的item列表。
如果我们不想使用默认的TextView显示ListView的一个item,而是要自己定义item的外观,怎么办?首先定义一个View的子类(SadItemView.java):
- package tommy.myandroid;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.widget.TextView;
- public class SadItemView extends TextView {
- public SadItemView(Context context) {
- super(context);
- }
-
- public SadItemView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public SadItemView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas); // draw text
- Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
- paint.setColor(0xffffffff);
- canvas.drawLine(0, getMeasuredHeight() / 2,
- getMeasuredWidth(), getMeasuredHeight() / 2,
- paint);
- }
- }
为了重用绘制文字的功能,SadItemView继承了TextView。类View有三个构造器,最好都定义好并调用父类的构造器。覆写onDraw可以自定义view的外观。
除了类文件,要在ListView里加入自己的item,还需要定义一个layout的xml(saditemview.xml):
- <?xml version="1.0" encoding="utf-8"?>
- <tommy.myandroid.SadItemView
- xmlns:android=""
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
注意saditemview.xml里只有一个结点,而且结点名与刚刚定义的类名一样。定义好java类和layou后,就可以在ListView里添加我们自定义的item了:
- String[] items = {"Item1", "Item2", "Item3"};
- //int itemResId = android.R.layout.simple_list_item_1;
- int itemResId = R.layout.saditemview;
-
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(
- this, itemResId, items);
- ListView lv = (ListView)findViewById(R.id.my_list_view);
- lv.setAdapter(adapter);
上面的代码使用了我们刚刚定义好的resource id,运行结果:
自定义一个全新的View
- class SimpleView extends View {
- public SimpleView(Context context) {
- super(context);
- }
- public SimpleView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public SimpleView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
-
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
- int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
- int measuredWidth = radius * 2;
- if (widthSpecMode == MeasureSpec.AT_MOST) {
- if (measuredWidth > widthSpecSize)
- measuredWidth = widthSpecSize;
- } else if (widthSpecMode == MeasureSpec.EXACTLY){
- measuredWidth = widthSpecSize;
- }
- int measuredHeight = radius * 2;
- switch (MeasureSpec.getMode(heightMeasureSpec)) {
- case MeasureSpec.AT_MOST:
- case MeasureSpec.EXACTLY:
- case MeasureSpec.UNSPECIFIED:
- measuredHeight = MeasureSpec.getSize(heightMeasureSpec);
- break;
- }
-
- setMeasuredDimension(measuredWidth, measuredHeight);
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- int width = getMeasuredWidth();
- int height = getMeasuredHeight();
- int r = Math.min(width, height) / 2;
- Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
- paint.setColor(color);
- canvas.drawCircle(width/2, height/2, r, paint);
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- color &= 0x00ffffff;
- if (color == 0x000000ff)
- color = 0x00ff0000;
- else
- color >>= 8;
- color |= 0xff000000; // alpha
- invalidate();
- return true;
- }
- private int radius = 5;
- private int color = 0xffff0000;
- }
自定义的View主要需要覆写OnMeaure方法和OnDraw方法(画一个圈)。上例的类还通过响应了onTouchEvent,并改变圆的颜色。可以使用setContentView来设置这个View。运行结果:
阅读(1245) | 评论(0) | 转发(0) |