如何从自定义视图中了解父填充?

时间:2013-11-28 11:09:41

标签: android android-layout android-custom-view

我创建了一个自定义视图并将其添加到布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:custom="http://schemas.android.com/apk/res/com.eleks.customview"
        android:padding="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <com.eleks.customview.CView
            android:id="@+id/circle1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            custom:textSize="@dimen/circle_text_size"
            custom:text="@string/circle_default_text"
            custom:radius="@dimen/circle_radius"
            custom:textColor="@color/circle_text"
            custom:textBackground="@color/circle_background" />

    </LinearLayout>

我在自定义视图中实现onMeasure并设置宽度和高度。但是我的视图忽略了父填充(20dp)。见图:

enter image description here

公共类CView扩展了View {     private static final String TAG =“CView”;

// default value
private static final int DEFAULT_RADIUS = 30;
private static final int DEFAULT_TEXT_SIZE = 12;
private static final int DEFAULT_TEXT_COLOR = Color.BLACK;
private static final int DEFAULT_BACKGROUND = Color.GRAY;
private static final int DEFAULT_WIDTH = DEFAULT_RADIUS * 2;
private static final int DEFAULT_HEIGHT = DEFAULT_RADIUS * 2;

// variables
private Paint mBackgroundPaint = null;
private Paint mTextPaint = null;
private float mCircleX = 0;
private float mCircleY = 0;
private float mTextX = 0;
private float mTextY = 0;
private int mWidth = DEFAULT_WIDTH;
private int mHeight = DEFAULT_HEIGHT;

// attrs
private int mTextColor = DEFAULT_TEXT_COLOR;
private int mBackground = DEFAULT_BACKGROUND;
private float mRadius = DEFAULT_RADIUS;
private String mText = null;
private float mTextSize = 0;

public CView(Context context)
{
    super(context);
    Log.d(TAG, "init view");
    init(null);
}

public CView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    Log.d(TAG, "init view");
    init(attrs);
}

private void init(AttributeSet attrs)
{
    if (attrs != null)
    {
        TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.CircleButton, 0, 0);
        try
        {
            mTextColor = a.getColor(R.styleable.CircleButton_textColor, DEFAULT_TEXT_COLOR);
            mBackground = a.getColor(R.styleable.CircleButton_textBackground, DEFAULT_BACKGROUND);
            mRadius = a.getDimension(R.styleable.CircleButton_radius, DEFAULT_RADIUS);
            mText = a.getString(R.styleable.CircleButton_text);
            mTextSize = a.getDimensionPixelOffset(R.styleable.CircleButton_textSize, DEFAULT_TEXT_SIZE);
        }
        finally
        {
            a.recycle();
        }
    }
    mBackgroundPaint = new Paint();
    mBackgroundPaint.setColor(mBackground);

    this.setMinimumHeight((int) mRadius * 2);
    this.setMinimumWidth((int) mRadius * 2);
}

private void initLayoutParams()
{
    LayoutParams params = this.getLayoutParams();
    params.width = mWidth + getLeft();
    params.height = mHeight + getTop();
    Log.d(TAG, "params| width: " + mWidth + " height: " + mHeight);
}

private void initText()
{
    mTextPaint = new Paint();
    mTextPaint.setColor(mTextColor);
    mTextPaint.setTextSize(mTextSize);
    Rect textRect = new Rect();
    if (mText == null)
    {
        mText = "";
    }
    mTextPaint.getTextBounds(mText, 0, mText.length(), textRect);
    mTextX = mCircleX - (((Math.abs(textRect.right) - Math.abs(textRect.left))) / 2);
    mTextY = mRadius + ((Math.abs(textRect.top) - (Math.abs(textRect.bottom))) / 2);
    Log.d(TAG, "text| x: " + mTextX + " y: " + mTextY);
}

@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
    Log.d(TAG, "init onDraw");

    if (canvas != null)
    {
        canvas.drawCircle(mCircleX, mCircleY, mRadius, mBackgroundPaint);
        // canvas.drawText(mText, mTextX, mTextY, mTextPaint);
    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    // super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    Log.d(TAG, "init onMeasure");
    setWidth(MeasureSpec.getMode(widthMeasureSpec), MeasureSpec.getSize(widthMeasureSpec));
    setHeight(MeasureSpec.getMode(heightMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
    mCircleX = getLeft() - getPaddingLeft() + mRadius;
    mCircleY = getTop() - getPaddingTop() + mRadius;
    fixedSize();
    Log.d(TAG, "circle| x: " + mCircleX + " y: " + mCircleY);
    this.setMeasuredDimension(mWidth, mHeight);
    // initText();

}

private void fixedSize()
{
    if (mWidth > mHeight)
    {
        mHeight = mWidth;
    }
    else if (mHeight > mWidth)
    {
        mWidth = mHeight;
    }
}

private void setWidth(final int modeWidth, final int width)
{
    switch (modeWidth)
    {
        case MeasureSpec.AT_MOST:
            Log.d(TAG, "width mode is AT_MOST");
            if (mRadius > 0)
            {
                mWidth = (int) (mRadius * 2) + getPaddingLeft() + getPaddingRight();
            }
            else
            {
                mWidth = DEFAULT_WIDTH;
            }
            mWidth = Math.min(mWidth, width);
            break;
        case MeasureSpec.EXACTLY:
            Log.d(TAG, "width mode is EXACTLY");
            break;
        case MeasureSpec.UNSPECIFIED:
        default:
            Log.d(TAG, "width mode is UNSPECIFIED");
            mWidth = DEFAULT_WIDTH;
            break;
    }
}

private void setHeight(final int modeHeight, final int height)
{
    switch (modeHeight)
    {
        case MeasureSpec.AT_MOST:
            Log.d(TAG, "height mode is AT_MOST");
            if (mRadius > 0)
            {
                mHeight = (int) (mRadius * 2) + getPaddingTop() + getPaddingBottom();
            }
            else
            {
                mHeight = DEFAULT_HEIGHT;
            }
            mHeight = Math.min(mHeight, height);
            break;
        case MeasureSpec.EXACTLY:
            Log.d(TAG, "height mode is EXACTLY");
            break;
        case MeasureSpec.UNSPECIFIED:
        default:
            Log.d(TAG, "height mode is UNSPECIFIED");
            mHeight = DEFAULT_HEIGHT;
            break;
    }
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
    // super.onLayout(changed, left, top, right, bottom);
    Log.d(TAG, "init onLayout");
    initLayoutParams();
}

如何获得父级填充?

2 个答案:

答案 0 :(得分:3)

使用getParent()但是如果你需要它就会有一些设计缺陷

答案 1 :(得分:0)

父视图的填充由父级测量。你不应该考虑它。但是,您应该考虑您的自定义小部件填充。代码不起作用,因为你的测量没有做它应该做的事情。我没有检查其余的代码,但可能你也犯了其他错误。

onMeasure()应该计算视图的大小,并将其设置为setMeasuredDimension()

如果您想开发自定义视图,请阅读官方文档。在这种情况下,onMeasure(int, int)方法文档。我也建议阅读how Android draw views

可能会查看github或某些标准Android视图中某些小部件的代码,以了解它们如何测量视图。 (LinearLayout和RelativeLayout很好,非常不同,ViewGroups的例子),而ImageView是一个很好的Widget完整示例。

即使您没有编写ViewGroup,它也能帮助您了解它对子视图的期望。这个想法是它可以(并且经常是)一个多步骤的过程,父母试图通过传递不同的约束来使孩子适应自己。

可以这样想:measureSpecs(你收到的参数)会告诉你是否应该占用你想要的所有空间,如果你应该适合特定的尺寸,或者只是特定的尺寸,宽度和高度。您的小部件应该遵循这些约束,并使用您计算的任何度量调用setMeasuredDimension()

在你的情况下,它应该是2 * radious + padding,除非它超过父视图给出的空间。

也就是说,如果你想绘制一个圆圈,自定义视图并不是真正的方法,但我想你只是想学习如何构建自定义视图。