Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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 如何在另一个自定义视图中添加自定义视图?_Android_Graphics_Android View_Android Custom View - Fatal编程技术网

Android 如何在另一个自定义视图中添加自定义视图?

Android 如何在另一个自定义视图中添加自定义视图?,android,graphics,android-view,android-custom-view,Android,Graphics,Android View,Android Custom View,我有一个自定义视图,希望在该自定义视图上再添加一个自定义视图。这是我的自定义视图类: public class CustomCircle extends View{ float radius; Paint paint = new Paint(); String message = ""; public CustomCircle(Context context, AttributeSet attrs) { super(context, attrs); } @Override pro

我有一个自定义视图,希望在该自定义视图上再添加一个自定义视图。这是我的自定义视图类:

public class CustomCircle extends View{

float radius;
Paint paint = new Paint();

String message = "";
public CustomCircle(Context context, AttributeSet attrs) {
    super(context, attrs);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawCircle(getWidth()/2, getHeight()/2,radius, paint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean isClickInCircle = false;
    float x = event.getX();
    float y = event.getY();

    double check = Math.sqrt((x-getWidth()/2)*(x-getWidth()/2) + (y-getHeight()/2)*(y-getHeight()/2));
    if (check<=radius) {
        isClickInCircle= true;
    }
    if (isClickInCircle) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            Toast.makeText(getContext(),message, Toast.LENGTH_LONG).show();
            return true;

        case MotionEvent.ACTION_UP:
            Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
            return true;

        default:
            break;
        }
    }

    return false;
}
我从主活动类调用B类,使用:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    B root = new B(this);
    root.addCircle();
    setContentView(root);
}
输出仅显示一个圆,而不是另一个圆内的圆。我的代码怎么了

输出只显示一个圆,而不是内部的圆 圈

如果要重叠子级,则选择了错误的布局,
RelativeLayout
FrameLayout
是最好的选择。此外,关于您的代码:

public class B extends RelativeLayout {
//...
public void addCircle() {

    // the constructor that uses the AttributeSet should be added if you use the 
    // custom component in the xml layout
    CustomCircle circleBlue = new CustomCircle(getContext());
    // ...
    // add it with LayoutParams
    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    rlp.addRule(RelativeLayout.CENTER_IN_PARENT);
    addView(circleBlue, rlp);
    // the same for the other view
}
此外,您的两个圆将具有相同的尺寸,因此它们将完全重叠(您将无法看到它们),您需要通过
LayoutParams
为它们指定不同的尺寸,例如:

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(200, 200);
对于第一个和:

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(100, 100);
第二个

输出只显示一个圆,而不是内部的圆 圈

如果要重叠子级,则选择了错误的布局,
RelativeLayout
FrameLayout
是最好的选择。此外,关于您的代码:

public class B extends RelativeLayout {
//...
public void addCircle() {

    // the constructor that uses the AttributeSet should be added if you use the 
    // custom component in the xml layout
    CustomCircle circleBlue = new CustomCircle(getContext());
    // ...
    // add it with LayoutParams
    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    rlp.addRule(RelativeLayout.CENTER_IN_PARENT);
    addView(circleBlue, rlp);
    // the same for the other view
}
此外,您的两个圆将具有相同的尺寸,因此它们将完全重叠(您将无法看到它们),您需要通过
LayoutParams
为它们指定不同的尺寸,例如:

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(200, 200);
对于第一个和:

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(100, 100);

对于第二个。

布局中是否可能有两个圆,但您只能看到一个 因为它和第一个圆圈的颜色相同,而且它们是相互重叠的

更改
圆圈.paint.setColor(Color.WHITE)
圆圈.paint.setColor(Color.BLACK)

看看它给出了什么

布局中可能有两个圆,但你只能看到一个吗 因为它和第一个圆圈的颜色相同,而且它们是相互重叠的

更改
圆圈.paint.setColor(Color.WHITE)
圆圈.paint.setColor(Color.BLACK)

看看它给了我什么

是的,我换了圆圈.paint.setColor(Color.BLACK);不过它还是给我显示了一个圆圈。我建议看一下@Luksprog answer,所以换一下颜色。你现在看到了什么?如果你看到的圆圈的颜色也被交换了,那么你有两个圆圈相互重叠。是的,我改变了circleRed.paint.setColor(Color.BLACK);不过它还是给我显示了一个圆圈。我建议看一下@Luksprog answer,所以换一下颜色。你现在看到了什么?如果您看到的圆的颜色也被交换,则有两个圆正好相互重叠。