所以我试图在xml布局之外创建自己的视图,并且很难让它显示任何内容。我确定我错过了一些简单的东西,但看不出它可能是什么。任何输入都表示赞赏。这是我用于测试目的的两个类。
public class TestSuite extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Turns off the application title at the top..
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Turns off the status bar at the top..
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new Background(this));
}
}
和
public class Background extends View {
public Background(Context context) {
super(context);
}
public Background(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Background(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Resources res = getResources();
Rect baseRectBounds = new Rect(0,0,200,200);
Rect topRectBounds = new Rect(20,20,160,160);
Rect bottomRectBounds = new Rect(40,40,120,120);
Paint baseColor = new Paint(res.getColor(R.color.blue));
Paint topColor = new Paint(res.getColor(R.color.red));
Paint bottomColor = new Paint(res.getColor(R.color.green));
canvas.drawRect(baseRectBounds, baseColor);
canvas.drawRect(topRectBounds, topColor);
canvas.drawRect(bottomRectBounds, bottomColor);
}
}
修改的
这是我正在使用的最新onDraw方法。
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Resources res = getResources();
Rect canvasBounds = canvas.getClipBounds();
Log.v("CanvasBounds before", "left - " + canvasBounds.left);
Log.v("CanvasBounds before", "top - " + canvasBounds.top);
Log.v("CanvasBounds before", "right - " + canvasBounds.right);
Log.v("CanvasBounds before", "bottom - " + canvasBounds.bottom);
Rect baseRect = new Rect(0,0,200,200);
Rect topRect = new Rect(20,20,160,160);
Rect botRect = new Rect(40,40,120,120);
Paint baseColor = new Paint(res.getColor(R.color.red));
Paint topColor = new Paint(res.getColor(R.color.green));
Paint botColor = new Paint(res.getColor(R.color.blue));
baseColor.setStyle(Style.FILL);
topColor.setStyle(Style.FILL);
botColor.setStyle(Style.FILL);
canvas.drawRect(baseRect, baseColor);
canvas.drawRect(topRect, topColor);
canvas.drawRect(botRect, botColor);
canvasBounds = canvas.getClipBounds();
Log.v("CanvasBounds after", "left - " + canvasBounds.left);
Log.v("CanvasBounds after", "top - " + canvasBounds.top);
Log.v("CanvasBounds after", "right - " + canvasBounds.right);
Log.v("CanvasBounds after", "bottom - " + canvasBounds.bottom);
}
Log语句的结果如下:
CanvasBounds before left - 0
CanvasBounds before top - 0
CanvasBounds before right - 480
CanvasBounds before bottom - 800
CanvasBounds after left - 0
CanvasBounds after top - 0
CanvasBounds after right - 480
CanvasBounds after bottom - 800
完成编辑
感谢Ted的帮助,这是我的最终onDraw方法(对于可能需要它的其他人)。
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Resources res = getResources();
Rect baseRect = new Rect(0,0,200,200);
Rect topRect = new Rect(20,20,160,160);
Rect botRect = new Rect(40,40,120,120);
Paint color = new Paint();
color.setColor(res.getColor(R.color.red));
canvas.drawRect(baseRect, color);
color.setColor(res.getColor(R.color.blue));
canvas.drawRect(topRect, color);
color.setColor(res.getColor(R.color.green));
canvas.drawRect(botRect, color);
}
答案 0 :(得分:1)
两个建议:
setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT))
(此处,LayoutParams
为ViewGroup.LayoutParams
)measure(int,int)
并按docs for View