我有一个具有绘制位图的自定义视图的应用程序。我想在位图顶部的视图中放置一个按钮。活动的 setContentView 设置为自定义视图类,因此我无法从 main.xml 中为按钮充气。我该如何解决这个问题?是否可以在视图类中以编程方式创建一个按钮,然后附加一个侦听器?
公共类 Jjilapp 扩展 Activity {
private static final String TAG = "*********jjil";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "***********inside oncreate about to set contentview = ");
setContentView(new TouchView(this));
}
class TouchView extends View{
public TouchView(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
.......
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inSampleSize = 1;
bm = BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length, bfo);
bgr = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
bgr = bm.copy(bm.getConfig(), true);
}// end of class touchView
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawBitmap(bgr, 0, 0, null);
canvas.drawCircle(centreX, centreY, radius,pTouch);
}//end of onDraw
}
}