2011-12-23 6 views
0

描画可能な形状からなるカスタムビューの寸法:私は実際にListViewコントロールでこれを使用していますhttp://developer.android.com/guide/topics/graphics/2d-graphics.html#shape-drawableで見つかった私はかなりのサンプルを、次の午前

public class CustomDrawableView extends View { 

    private ShapeDrawable mDrawable; 

    public CustomDrawableView(Context context, AttributeSet attr) { 
    super(context, attr); 

    int x = 10; 
    int y = 10; 
    int width = 300; 
    int height = 50; 

    mDrawable = new ShapeDrawable(new OvalShape()); 
    mDrawable.getPaint().setColor(0xff74AC23); 
    mDrawable.setBounds(x, y, x + width, y + height); 
    } 

    protected void onDraw(Canvas canvas) { 
    mDrawable.draw(canvas); 
    } 
    } 

    // XML snippet where I am using this custom view 
    <com.example.shapedrawable.CustomDrawableView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

、それは(私のアダプタのGetViewメソッドで膨らませます私は非カスタムビューのために見えるので、この作品が分かります)。

layout_widthとlayout_heightに "dp"値を指定しない限り、描画されないことに気付きました。しかし、layout_widthとlayout_heightに500dpのようなものを任意に使用すると、それが表示されます。 私は非常に混乱しています - なぜwrap_contentを使用するときにコードで指定する高さ/幅のディメンションを使用していませんか?

答えて

1

私はonMeasureをオーバーライドして、私のドロウアブルの測定値を返さなければならなかったことが判明しました。私は例としてLabelView APIDemoを使用しました。

関連する問題