1

そのため、xml レイアウトの外に独自のビューを作成しようとしていますが、何かを表示するのに苦労しています。シンプルなものが欠けていると確信していますが、それが何であるかはわかりません。どんな入力でも大歓迎です。テスト目的で使用している 2 つのクラスを次に示します。

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);
}
4

1 に答える 1

1

2 つの提案:

  1. あなたのc'torで、電話してくださいsetLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT))(ここでは、LayoutParamsですViewGroup.LayoutParams
  2. のドキュメントでmeasure(int,int)提案されているようにオーバーライドして実装しますView
于 2011-05-30T00:27:09.983 に答える