1

私は次のようなカスタムビューを持っています:

public class CustomView extends View {

    protected Context c;
    protected String text;
    ... // and some more useful member variables...

    public CustomView(String text, Context c, ...) {

         this.text = text; this.c = c;
         ...
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        LinearLayout ll = new LinearLayout(c);
        TextView tv = new TextView(c);
        tv.setText(text);
        ll.addView(tv);

        ll.draw(canvas);
    }

そして私の主な活動では、これを行います:

    RelativeLayout gamelayout = (RelativeLayout) findViewById(R.id.gamelayout);

    CustomView customview = new CustomView("Textview text", this);
    gamelayout.addView(customview);

私の問題は、単に何も描画されないということです。描画されたTextViewは「gamelayout」に表示されません。私は何が間違っているのですか?

4

2 に答える 2

1

TextView オブジェクトは、キャンバスに直接描画することはできません。レイアウトに割り当ててからこれを行う必要があるためです。

ll.layout(0, 0, canvas.getWidth(), canvas.getHeight()); //Modify values as needed

個人的には、 の呼び出しでエラーがスローされないことに驚いていll.draw()ます。TextView を描画する必要がない限り、テキストをキャンバスに描画することをお勧めします。

canvas.drawText(...)

こちらのドキュメントを参照してください

于 2013-01-23T20:20:22.023 に答える
1

あなたLinearLayoutはあなたのビューに添付されていません

LinearLayoutthis.addView(ll)をビューに追加してみてください。

于 2013-01-23T20:58:12.893 に答える