具有自定义视图背景的对话框 [英] Dialog with custom view background
问题描述
已解决:请参阅下面的答案
Solved: see answer below
我正在对 Dialog 进行子类化以创建具有自定义背景的对话框.我在对话框中添加了一个子类 View ,它正在正确绘制位图背景和布局.但是按钮不会响应任何触摸事件.
I am sub-classing Dialog to create a dialog with a custom background. I have added a subclass View to the Dialog and it is drawing the bitmap background and layout correctly. But the buttons will not respond to any touch events.
我怀疑必须在 Dialog 类中加载 LinearLayout,但我认为我必须在视图类中加载它才能在位图上绘制.
I suspect the LinearLayout has to be loaded in the Dialog class, but I think I have to load it in the view class to draw on top of the bitmap.
我是 Android 开发者的新手,所以我为这个问题道歉.这是我正在做的:
I am totally new to Android dev, so I apologize for the question. Here is what I am doing:
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 子类时,我必须禁用此代码.但是按钮应该仍然显示没有侦听器的点击状态.
This is how I am setting the listeners. I have to disable this code when using the View subclass as shown. But the buttons should still show the click state without a listener which they don't.
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);
}
});
推荐答案
我不需要添加子视图类和绘制背景,只需添加:
Instead of adding the subview class and drawing the background, all I needed to do was add:
getWindow().setBackgroundDrawableResource(R.drawable.dialog_bg);
我想我只是太努力了!
这篇关于具有自定义视图背景的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!