Android相对布局的孩子有0个大小

时间:2014-05-28 06:07:58

标签: android android-layout subview android-relativelayout

我有一个自定义RelativeLayout,我正在尝试添加子视图。问题是没有任何子视图正在绘制。我已覆盖dispatchDraw函数以打印出子视图的大小,所有值都返回0(左,上,右,下)。 addText函数由外部类调用;无法绘制TextViewImageView

ScreenSpaceView类在屏幕上绘制一个框,框外面显示为灰色。

public class ScreenSpaceView extends RelativeLayout
{
    private Paint mPaint = new Paint();
    private Rect mDrawArea;
    private float mZoomDistance;
    private CountDownTimer mDoubleTapTimer;
    private int mTapCount;
    private float mPercentage = 1.0f;
    private Context mContext;

    public ScreenSpaceView(Context context)
    {
        super(context);
        setup(context);
    }

    public ScreenSpaceView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        setup(context);
    }

    public ScreenSpaceView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        setup(context);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        Log.e("onLayout", "" + changed);
    }

    private void setup(Context aContext)
    {
        if (!isInEditMode())
            mMode = Mode.None;

        mContext = aContext;
        setWillNotDraw(false);

        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(3);

        mDrawArea = new Rect(0, 0, 1, 1);

        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void dispatchDraw(Canvas canvas)
    {
        Log.e("Child Count", "" + getChildCount());
        int cnt = getChildCount();
        for (int i = 0; i < cnt; i++)
        {
            View child = getChildAt(i);

            int l = child.getLeft();
            int t = child.getTop();
            int r = child.getRight();
            int b = child.getBottom();

            Log.e("Child Count", "" + l + " " + t + " " + r + " " + b);
        }

        super.dispatchDraw(canvas);
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        Log.e("onDraw", "" + getChildCount());
        if (isInEditMode())
        {
            super.onDraw(canvas);
            return;
        }

        Rect drawArea = new Rect(mDrawArea);
        int xDiff = (drawArea.right - (int)((float)(drawArea.right - drawArea.left) * mPercentage)) / 2;
        int yDiff = (drawArea.bottom - (int)((float)(drawArea.bottom - drawArea.top) * mPercentage)) / 2;
        drawArea.left += xDiff;
        drawArea.top += yDiff;
        drawArea.right -= xDiff;
        drawArea.bottom -= yDiff;

        canvas.clipRect(drawArea, Region.Op.DIFFERENCE);
        canvas.drawARGB(125, 0, 0, 0);
        canvas.clipRect(new Rect(0, 0, canvas.getWidth(), canvas.getHeight()), Region.Op.REPLACE);

        canvas.drawRect(drawArea.left, drawArea.top, drawArea.right, drawArea.bottom, mPaint);

        super.onDraw(canvas);
    }

    public void setDrawArea(Rect aRect)
    {
        mDrawArea = aRect;
    }

    public void setDrawColour(int aColor)
    {
        mPaint.setColor(aColor);
    }

    public void addText()
    {
        Log.e("Add", "Added");

        TextView view = new TextView(mContext);
        view.setText("Hello World!");

        view.setTextColor(Color.WHITE);
        view.setBackgroundColor(view.getResources().getColor(R.color.Green));

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.height = 150;
        params.width = 150;
        view.setLayoutParams(params);
        addView(view, params);
        Log.e("Add", "Added ...");

        ImageView image = new ImageView(mContext);
        image.setImageDrawable(getResources().getDrawable(R.drawable.one_to_one));
        image.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        addView(image);

        invalidate();
    }
}

这是布局文件。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/Black">

    <ImageView
        android:id="@+id/image"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:scaleType="matrix"/>

    <com.hdms.manager.PhotoEditor.ScreenSpaceView
        android:id="@+id/drawLayer"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

    <include layout="@layout/crop_photo" />
    <include layout="@layout/text_photo" />

</RelativeLayout>

任何想法都将不胜感激。我尝试在TextView添加对齐但没有成功。以下是更新后的代码。

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
view.setLayoutParams(params);

2 个答案:

答案 0 :(得分:0)

尝试设置layout_align *属性。

答案 1 :(得分:0)

所以我仍然不知道为什么文本没有显示,但我通过将ScreenSpaceView设为View并将其插入相对布局来解决它。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/textOverlay">

    <com.hdms.manager.PhotoEditor.ScreenSpaceView
        android:id="@+id/drawLayer"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

</RelativeLayout>

和代码:

public class ScreenSpaceView extends View
{
    ...
    private RelativeLayout mRelativeOverlay;

    public void addText()
    {
        if (mRelativeOverlay == null)
            return;

        TextView text = new TextView(getContext());
        text.setText("Hello World!");
        text.setTextColor(text.getResources().getColor(R.color.White));

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        text.setLayoutParams(params);

        mRelativeOverlay.addView(text, params);
        mRelativeOverlay.invalidate();
    }
}