我在一些Android教程之后写了一堂课。该类存储为 " TodoList的\ SRC \ com.example.todolist \ ToDoListItemView.java"
package com.example.todolist;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;
public class ToDoListItemView extends TextView{
private Paint marginPaint,linePaint;
private int paperColor;
private float margin;
public ToDoListItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public ToDoListItemView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ToDoListItemView(Context context) {
super(context);
init();
}
private void init(){
Resources myResources = getResources();
//Create the paint resources to be used in the onDraw method
marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
marginPaint.setColor(myResources.getColor(R.color.notepad_margin));
linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
linePaint.setColor(myResources.getColor(R.color.notepad_lines));
//Get paper background color and margin width
paperColor = myResources.getColor(R.color.notepad_paper);
margin = myResources.getDimension(R.dimen.notepad_margin);
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(paperColor);
//Draw ruled lines
canvas.drawLine(0, 0, 0, getMeasuredHeight(), linePaint);
canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint);
//Draw margin
canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);
//Move text across from the margin
canvas.save();
canvas.translate(margin, 0);
//Render
super.onDraw(canvas);
canvas.restore();
}
}
当我在中创建新布局时 " TodoList的\ RES \布局" 任何名字,我输入" com。",它会自动推荐我的路径 " com.example.todolist.ToDoListItemView" 我想知道我的" ToDoListItemView.java"中有什么区别特征。让日食认为它是一种布局?
答案 0 :(得分:0)
建议ToDoListItemView
,因为它是CustomView
。
Eclipse认识到您的班级ToDoListItemView
派生自TextView
派生自 View
。
所有扩展View
的类都可以在布局中使用。
如果您的课程没有以某种方式延伸View
,则无法将其放入布局。