解决:见下面的答案
我正在对Dialog进行子类化以创建具有自定义背景的对话框。我已经在Dialog中添加了一个子类View,它正确地绘制了位图背景和布局。但按钮不会响应任何触摸事件。
我怀疑必须在Dialog类中加载LinearLayout,但我想我必须在视图类中加载它以在位图之上绘制。
我是Android开发人员的新手,所以我为这个问题道歉。这就是我在做的事情:
public class CustomDialog extends Dialog {
private static final String TAG = "CustomDialog";
private static int layoutWidth = 640;
private static int layoutHeight = 400;
public CustomDialog(Context context) {
super(context, android.R.style.Theme_Translucent_NoTitleBar);
requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutParams params = getWindow().getAttributes();
params.width = LayoutParams.FILL_PARENT;
getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
// setContentView(R.layout.layout_dialog); // This works fine, the buttons work
setContentView(new NewLayoutDialogView(context));
}
public static class NewLayoutDialogView extends View {
private Drawable bg;
public LinearLayout layout;
private OnColorChangedListener mListener;
public interface OnBrushChangedListener {
void brushChanged(float radius);
}
NewLayoutDialogView(Context context) {
super(context);
InputStream stream = getResources().openRawResource(R.drawable.dialog_bg);
bg = NinePatchDrawable.createFromStream(stream, null);
layout = (LinearLayout) LinearLayout.inflate(context, R.layout.layout_dialog, null);
Button ok = (Button) layout.findViewById(R.id.ok_button);
layout.setWillNotDraw(false);
layout.setVisibility(View.VISIBLE);
setVisibility(View.VISIBLE);
layout.measure(layoutWidth, layoutHeight);
layout.layout(0, 0, layoutWidth, layoutHeight);
}
@Override
protected void onDraw(Canvas canvas){
if (bg != null) {
bg.setBounds(10, 0, canvas.getWidth(), canvas.getHeight());
bg.draw(canvas);
}
layout.draw(canvas);
}
}
}
编辑:这就是我设置监听器的方式。我必须在使用View子类时禁用此代码,如图所示。但是按钮仍然应该显示没有监听器的点击状态,而不是。
Dialog dialog = new ChangeLayoutDialog(getActivity());
Button cancel = (Button) dialog.findViewById(R.id.cancel_button);
cancel.setTypeface(font);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
Button ok = (Button) dialog.findViewById(R.id.ok_button);
ok.setTypeface(font);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
setCellLayout(layoutFile);
}
});
答案 0 :(得分:1)
不是添加子视图类和绘制背景,我需要做的就是添加:
getWindow().setBackgroundDrawableResource(R.drawable.dialog_bg);
我想我只是太努力了!