2011-05-30 74 views
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); 
} 

回答

1

两个建议:

    在c'tor
  1. ,调用setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT))(这里,LayoutParamsViewGroup.LayoutParams
  2. 覆盖measure(int,int)并实现其作为docs for View
+0

好建议..所以我增加了'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

+0

呃...我的'ViewGroup.FILL_PARENT'上面应该有'LayoutParams.FILL_PARENT'。在我的部分错别字。 – 2011-05-30 19:38:05

+0

如果它仍未显示,请尝试使用[Hierarchy Viewer](http://developer.android.com/guide/developing/tools/hierarchy-viewer.html)检查布局是否在模拟器中运行。它可能有助于诊断这是布局问题还是渲染问题。 – 2011-05-30 20:18:23

相关问题