Android 如何将自定义视图添加到布局中,以便与父视图匹配?

Android 如何将自定义视图添加到布局中,以便与父视图匹配?,android,Android,我有以下自定义视图(基本上是一个包含图像的视图,您可以在该图像上绘制矩形) 我试过: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:ori

我有以下自定义视图(基本上是一个包含图像的视图,您可以在该图像上绘制矩形)

我试过:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="1" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.8"
        android:orientation="vertical"
        android:weightSum="1" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.9" >

            <HorizontalScrollView
                android:id="@+id/horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <ScrollView
                    android:id="@+id/vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <com.example.test.MyView
                        android:id="@+id/image"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                      </ScrollView>
            </HorizontalScrollView>
        </LinearLayout>

但是没有成功

可绘制的图像不显示


它显示一个空白字段…

您应该在自定义视图中覆盖
onMeasure()

@Override
public void onMeasure(int widthSpecs, int heightSpecs) {
    super.onMeasure(widthSpecs, heightSpecs);
    setMeasuredDimension(mDisplayUtils.getScreenWidth(), mDisplayUtils.getScreenHeight());
}
或者任何你想要的价值观

我能想到的另一件事是,在主构造函数上初始化变量。这样做:

public MyView(Context c) {
    super(c);
    init();
}

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public MyView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init(){
    Log.e("","call first constructor");
    mPath = new Rect();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(0xFFFF0000);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(3);
}

感谢Android开发者也许我明白了你的观点,但是我应该在测量中加入什么来允许匹配父项呢?…尝试了
@覆盖测量上的公共空白(int-widthSpecs,int-heightSpecs){setMeasuredDimension(MeasureSpec.getSize(widthSpecs),MeasureSpec.getSize(heightSpecs));}
但是没有成功
@Override
public void onMeasure(int widthSpecs, int heightSpecs) {
    super.onMeasure(widthSpecs, heightSpecs);
    setMeasuredDimension(mDisplayUtils.getScreenWidth(), mDisplayUtils.getScreenHeight());
}
public MyView(Context c) {
    super(c);
    init();
}

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public MyView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init(){
    Log.e("","call first constructor");
    mPath = new Rect();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(0xFFFF0000);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(3);
}