1
所以我試圖創建自己的視圖之外的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);
}
日誌語句的結果如下:
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
完成編輯
由於泰德的幫助,這是我最後的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);
}
好建議..所以我增加了'setLayoutParams(新的LayoutParams(ViewGroup.FILL_PARENT,ViewGroup.FILL_PARENT));'到所有3個構造函數中,然後overrode'onMeasure(int,int)'(文檔說你必須重寫onMeasure)'super.onMeasure(widthMeasureSpec,heightMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimensions(width,height);'但它仍然顯示爲空白屏幕。我甚至添加了一些Log.v語句來查看返回的寬度和高度,並且它們是正確的值。 :T – 2011-05-30 04:48:44
呃...我的'ViewGroup.FILL_PARENT'上面應該有'LayoutParams.FILL_PARENT'。在我的部分錯別字。 – 2011-05-30 19:38:05
如果它仍未顯示,請嘗試使用[Hierarchy Viewer](http://developer.android.com/guide/developing/tools/hierarchy-viewer.html)檢查佈局是否在模擬器中運行。它可能有助於診斷這是佈局問題還是渲染問題。 – 2011-05-30 20:18:23