安卓开发,添加多个自定义View,只显示了一个
代码如下:自定义的Viewpackagecom.example.helloworld;importandroid.content.Context;importandroi...
代码如下:
自定义的View
package com.example.helloworld;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class DrawView extends View{
public float currentX;
public float currentY;
Paint p = new Paint();
public DrawView(Context context, float x, float y){
super(context);
this.currentX = x;
this.currentY = y;
}
public DrawView(Context context){
super(context);
}
public DrawView(Context context, AttributeSet set){
super(context, set);
}
public void onDraw(Canvas canvas){
super.onDraw(canvas);
p.setColor(Color.RED);
canvas.drawCircle(currentX, currentY, 15, p);
}
}
----------------------------------------------------------------------------------
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class HelloWorldActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_world);
LinearLayout root = (LinearLayout)findViewById(R.id.root);
final DrawView draw = new DrawView(this,200,400);
root.addView(draw);
final DrawView draw2 = new DrawView(this,100,200);
root.addView(draw2);
}
}
----------------------------------------------------------------------------
XML文件只有一个LinearLayout布局,不复制出来了。
运行结果:
结果只有一个小球,我希望能有2个,怎么解决这个问题,新手入门紧急,感谢谢谢了!! 展开
自定义的View
package com.example.helloworld;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class DrawView extends View{
public float currentX;
public float currentY;
Paint p = new Paint();
public DrawView(Context context, float x, float y){
super(context);
this.currentX = x;
this.currentY = y;
}
public DrawView(Context context){
super(context);
}
public DrawView(Context context, AttributeSet set){
super(context, set);
}
public void onDraw(Canvas canvas){
super.onDraw(canvas);
p.setColor(Color.RED);
canvas.drawCircle(currentX, currentY, 15, p);
}
}
----------------------------------------------------------------------------------
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class HelloWorldActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_world);
LinearLayout root = (LinearLayout)findViewById(R.id.root);
final DrawView draw = new DrawView(this,200,400);
root.addView(draw);
final DrawView draw2 = new DrawView(this,100,200);
root.addView(draw2);
}
}
----------------------------------------------------------------------------
XML文件只有一个LinearLayout布局,不复制出来了。
运行结果:
结果只有一个小球,我希望能有2个,怎么解决这个问题,新手入门紧急,感谢谢谢了!! 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询