动态复合视图(在自定义视图下方查看)

时间:2015-08-04 09:10:08

标签: android view android-custom-view

我有CircleView自定义对象,工作正常。

enter image description here

我希望将其与下面的TextView复合,如下所示:

enter image description here

所以我制作了CircleCheckBoxWithText来复合两个视图,

问题是如果我添加的第一个视图是我的自定义视图,而第二个视图是TextView,我看不到TextView。

(当我添加的第一个视图是TextView,第二个是我的自定义视图时,我可以看到两个视图)

CircleView:

public class CircleView extends View
{
    private float mDimension;
    private int mFillColor;
    private int mBorderColor;
    private boolean mIsChecked;
    private Paint mFillPaint = new Paint();
    private Paint mStrokePaint = new Paint();

    public CircleView(Context context)
    {
        super(context);
    }

    public CircleView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
    }

    public CircleView(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        // Get the styledAttributes from the xml
        TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CircleView, 0, 0);

        try
        {
            mDimension = styledAttributes.getDimensionPixelOffset(R.styleable.CircleView_dim, 20);
            mFillColor = styledAttributes.getColor(R.styleable.CircleView_fill_color, Color.WHITE);
            mBorderColor = styledAttributes.getColor(R.styleable.CircleView_border_color, Color.WHITE);
            mIsChecked = styledAttributes.getBoolean(R.styleable.CircleView_checked, false);
        }
        finally
        {
            styledAttributes.recycle();
        }
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

        // init canvas
        canvas.drawARGB(0, 0, 0, 0);

        // Set the Fill
        mFillPaint.setAntiAlias(true);
        mFillPaint.setDither(true);
        mFillPaint.setColor(mFillColor);

        // Set the Stroke
        mStrokePaint.setAntiAlias(true);
        mStrokePaint.setDither(true);
        mStrokePaint.setColor(mBorderColor);
        mStrokePaint.setStyle(Paint.Style.STROKE);
        mStrokePaint.setStrokeWidth(8f);

        canvas.drawCircle(mDimension / 2, mDimension / 2, mDimension / 2 - 0.2f, mFillPaint);

        if(mIsChecked)
        {
             canvas.drawCircle(mDimension / 2, mDimension / 2, mDimension / 2 - mStrokePaint.getStrokeWidth() / 2 + 0.1f, mStrokePaint);
        }
    }
}

CircleCheckBoxWithText:

public class CircleCheckBoxWithText extends LinearLayout
{
    public CircleCheckBoxWithText(Context context)
    {
        super(context);
        setOrientation(VERTICAL);
    }

    public CircleCheckBoxWithText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        setOrientation(VERTICAL);

        addView(new CircleView(context, attrs));
        TextView textView = new TextView(context);
        textView.setGravity(Gravity.CENTER);
        textView.setText("TEST");
        addView(textView);
    }

    public CircleCheckBoxWithText(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        setOrientation(VERTICAL);
    }
}

0 个答案:

没有答案