所以我有一个名为 Square 的自定义视图,我想将其添加到表格中。当我用文本视图替换我的方块时,它可以完美地工作。
我在网上搜索了很多,发现我应该在构造函数中调用 setWillNotDraw(false) 。我这样做没有结果。
这是我的班级广场:
public class Square extends View
{
private String courseName;
private String className;
private String place;
private Weekdays weekday;
private int hour;
private Paint paint;
public Square(Context context, AttributeSet attrs){
super(context,attrs);
setWillNotDraw(false);
}
public Square(Context context, String courseName, String className, String place, Weekdays weekday, int hour)
{
super(context);
this.courseName = courseName;
this.className = className;
this.place = place;
this.weekday = weekday;
this.hour = hour;
setWillNotDraw(false);
paint = new Paint();
}
public Square(Context context,Weekdays weekdays, int i) {
super(context);
this.weekday = weekdays;
this.hour = i;
setWillNotDraw(false);
paint = new Paint();
paint.setColor(Color.BLACK);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawText("Hello I'm a test", 1, 30, paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
我尝试将 onMeasure() 方法设置为使用 10,10 或其他任何值,但没有显示任何内容。我不知道这是否与我的问题有关。
该表是这样动态制作的:
public class MainActivity extends Activity {
private Controller controller;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
controller = new Controller(this);
createTable();
}
private void createTable() {
// get a reference for the TableLayout
TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
/**Create 8 rows*/
for(int i=0;i<Controller.TABLE_ROW_COUNT;i++){
// create a new TableRow
TableRow row = new TableRow(this);
/** create 5 columns*/
for(int j=0;j<Controller.TABLE_COLUMN_COUNT;j++){
Square sq = controller.tableModel.getSquare(Weekdays.values()[j], i);
row.addView(sq, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
// add the TableRow to the TableLayout
table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
}
谢谢!