我想构建我自己的自定义视图,它应该看起来像Crysis-GUI。
首先,我设计了一个基于XML的布局,并通过setContentView(int resid)-Method使其可见。工作得很好。
但是现在我不想更进一步,画出我的布局。所以我创建了一个新类,让它扩展View并覆盖onDraw() - Method。到现在为止还挺好。按预期工作
public class RifleView extends View {
public RifleView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint p = new Paint();
p.setARGB(255, 255, 0, 0);
canvas.drawText("Hello World", 20, 20, p);
}
}
但是我怎么还能使用我的XML-Layout?我不能再做setContentView了,那么如何实现相同的效果?
答案 0 :(得分:2)
为什么不能使用setContentView?只需创建一个xml标记:<com.mycompany.mypackage.myComponent ... xml attributes an tags </com.mycompany.mypackage.myComponent>