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,所以换一下颜色。你现在看到了什么?如果您看到的圆的颜色也被交换,则有两个圆正好相互重叠。