Dialog with custom view background

旧城冷巷雨未停 提交于 2020-01-20 08:34:54

问题


Solved: see answer below

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.

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.

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);
    }
 }
}

Edit: 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);
             }
          });

回答1:


Instead of adding the subview class and drawing the background, all I needed to do was add:

getWindow().setBackgroundDrawableResource(R.drawable.dialog_bg);

I guess I was just trying way too hard!



来源:https://stackoverflow.com/questions/8566678/dialog-with-custom-view-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!