Android相对布局子项的大小为0

Android相对布局子项的大小为0,android,android-layout,subview,android-relativelayout,Android,Android Layout,Subview,Android Relativelayout,我有一个自定义的RelativeLayout,我正试图向其中添加子视图。问题是没有一个子视图是绘制的。我已经覆盖了dispatchDraw函数来打印子视图的大小,所有值都返回0(左、上、右、下)。addText函数由外部类调用;既不绘制文本视图也不绘制图像视图 ScreenSpaceView类在屏幕上绘制一个框,框外显示为灰色 public class ScreenSpaceView extends RelativeLayout { private Paint mPaint = new

我有一个自定义的
RelativeLayout
,我正试图向其中添加子视图。问题是没有一个子视图是绘制的。我已经覆盖了
dispatchDraw
函数来打印子视图的大小,所有值都返回0(左、上、右、下)。
addText
函数由外部类调用;既不绘制
文本视图
也不绘制
图像视图

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();
    }
}

尝试设置布局对齐*属性

所以我仍然不知道为什么文本没有显示,但我通过将ScreenSpace视图设置为一个视图并将其插入到一个相对布局中来绕过它

<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>

凭什么?文本还是相对的布局?
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
view.setLayoutParams(params);
<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();
    }
}