对话与自定义视图背景 [英] Dialog with custom view background

查看:236
本文介绍了对话与自定义视图背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决:请参阅下面的

答案

我是子类对话框来创建一个自定义背景的对话框。我添加了一个子类视图对话框,它是正确绘制位图背景和布局。但是,按钮将不会对任何触摸事件。

我怀疑的LinearLayout有对话框类加载,但我想我必须加载它在视图类上绘制位图的顶部。

我是全新的Andr​​oid开发人员,所以我对这个问题表示歉意。下面是我在做什么:

 公共类CustomDialog扩展对话框{私有静态最后弦乐TAG =CustomDialog;
私有静态诠释layoutWidth = 640;
私有静态诠释layoutHeight = 400;公共CustomDialog(上下文的背景下){    超(背景下,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); //这工作得很好,按键的工作
    的setContentView(新NewLayoutDialogView(上下文));
}公共静态类NewLayoutDialogView扩展视图{    私人可绘制BG;
    公众的LinearLayout布局;
    私人OnColorChangedListener mListener;    公共接口OnBrushChangedListener {
        无效brushChanged(浮点半径);
    }    NewLayoutDialogView(上下文的背景下){        超级(上下文);        。InputStream的流= getResources()openRawResource(R.drawable.dialog_bg);
        BG = NinePatchDrawable.createFromStream(流,NULL);        布局=(的LinearLayout)LinearLayout.inflate(背景下,R.layout.layout_dialog,NULL);        OK按钮=(按钮)layout.findViewById(R.id.ok_button);        layout.setWillNotDraw(假);        layout.setVisibility(View.VISIBLE);
        setVisibility(View.VISIBLE);        layout.measure(layoutWidth,layoutHeight);
        layout.layout(0,0,layoutWidth,layoutHeight);
    }    @覆盖
    保护无效的onDraw(帆布油画){        如果(BG!= NULL){
          bg.setBounds(10,0,canvas.getWidth(),canvas.getHeight());
          bg.draw(画布);
        }        layout.draw(画布);
    }
 }
}

编辑:这是我如何设置监听器。我必须禁用此code。使用如图所示的视图子类时。但按键还是应该表现出不,他们没有一个听众点击状态。

 对话对话框=新ChangeLayoutDialog(getActivity());        按钮取消=(按钮)dialog.findViewById(R.id.cancel_button);
        cancel.setTypeface(字体);
        cancel.setOnClickListener(新View.OnClickListener(){
          公共无效的onClick(视图v){
              dialog.dismiss();
             }
          });        OK按钮=(按钮)dialog.findViewById(R.id.ok_button);
        ok.setTypeface(字体);
        ok.setOnClickListener(新View.OnClickListener(){
          公共无效的onClick(视图v){
              dialog.dismiss();
              setCellLayout(layoutFile);
             }
          });


解决方案
,而不是添加子视图类,并绘制背景的

,所有我需要做的是补充:

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

我想我只是想这样太辛苦了!

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

解决方案

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!

这篇关于对话与自定义视图背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆