Android 以编程方式改为使用xml进行布局
我有这门课 MyTouchEventView.javaAndroid 以编程方式改为使用xml进行布局,android,android-layout,android-view,Android,Android Layout,Android View,我有这门课 MyTouchEventView.java import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.view.MotionEvent; im
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
public class MyTouchEventView extends View {
private Paint paint = new Paint();
private Path path = new Path();
private Path circlePath = new Path();
public Button btnReset;
public LayoutParams params;
public MyTouchEventView(Context context) {
super(context);
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(4f);
btnReset = new Button(context);
btnReset.setText("Clear Screen");
params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
btnReset.setLayoutParams(params);
btnReset.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
path.reset();
postInvalidate();
}
});
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float pointX = event.getX();
float pointY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX, pointY);
circlePath.reset();
circlePath.addCircle(pointX, pointY, 30, Path.Direction.CW);
break;
case MotionEvent.ACTION_UP:
circlePath.reset();
break;
default:
return false;
}
postInvalidate();
return true;
}
}
DrawingBrush.java
public class DrawingBrush extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyTouchEventView tv = new MyTouchEventView(this);
setContentView(tv);
addContentView(tv.btnReset, tv.params);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
现在我想转换这个
MyTouchEventView tv = new MyTouchEventView(this);
setContentView(tv);
addContentView(tv.btnReset, tv.params);
作为setContentView(R.layout.main)
并将按钮和视图中的其他组件放在“main.xml”布局中
我该怎么做?希望有人能理解我的意思。
MyTouchEventView
应该至少还有一个构造函数。
除了上下文
之外,此构造函数还应接受属性集
参数
public MyTouchEventView (Context context, AttributeSet attrs) {
// perform initialization
}
当视图在XML布局文件中声明时,它将被调用
然后,您将用XML声明视图,如下所示:
<com.my_package_name.MyTouchEventView
....
/>
Android文档中详细介绍了此主题。看看这里:我想删除MyTouchEventView类,并将其中的内容放入类DrawingBrush中。我不确定我是否理解您刚才所说的内容。但是,如果需要在
DrawingBrush
中使用MyTouchEventView
,请通过添加一个构造函数修改MyTouchEventView
类,创建一个新的布局XML文件,在该XML文件中声明MyTouchEventView
,然后在DrawingBrush
中使用此XML文件。如果你对自定义视图不熟悉,我强烈建议你暂停一下,先阅读文档。谢谢你的回答,这个例子对我也有帮助