Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将自定义矩形添加到自定义RelativeLayout_Android - Fatal编程技术网

Android 将自定义矩形添加到自定义RelativeLayout

Android 将自定义矩形添加到自定义RelativeLayout,android,Android,我有一个自定义的RelativeLayout,在这里我想动态创建矩形 它们没有显示在我当前的代码中,但我不知道原因是什么 自定义相对长度: public class DrawView extends RelativeLayout { public DrawView(Context context) { super(context); setFocusable(true); this.addView(new Rectangle(this.

我有一个自定义的RelativeLayout,在这里我想动态创建矩形

它们没有显示在我当前的代码中,但我不知道原因是什么

自定义相对长度:

public class DrawView extends RelativeLayout {

    public DrawView(Context context) {
        super(context);
        setFocusable(true);

        this.addView(new Rectangle(this.getContext()));
    }

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

    }
自定义矩形:

public class Rectangle extends View {

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

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

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

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);

        canvas.drawRect(new Rect(), paint);

    }

}
编辑: 解决办法是:

public class Rectangle extends View {

    public Rectangle(Context context) {
        super(context);
        init();
    }

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

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

    private void init() {
        this.setBackgroundResource(R.drawable.rectangle);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = 150;
        int height = 50;

        setMeasuredDimension(width, height);
    }

}


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="5sp"/>
    <gradient
        android:angle="315"
        android:startColor="#FFFF6666"
        android:endColor="#FFFF1111"
        android:type="linear"
        />
</shape>
公共类矩形扩展视图{
公共矩形(上下文){
超级(上下文);
init();
}
公共矩形(上下文、属性集属性){
超级(上下文,attrs);
init();
}
公共矩形(上下文上下文,属性集属性,int defStyle){
超级(上下文、属性、定义样式);
init();
}
私有void init(){
这个.setBackgroundResource(R.drawable.rectangle);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
超级测量(宽度测量、高度测量);
整数宽度=150;
整数高度=50;
设置测量尺寸(宽度、高度);
}
}

我认为您没有设置形状的
布局参数。尝试创建一个
矩形
对象,设置其宽度+高度,然后将其添加到您的
RelativeLayout
:D中