2

解決済み:以下の回答を参照してください

カスタムの背景を持つダイアログを作成するために、Dialogをサブクラス化しています。ダイアログにサブクラスViewを追加しましたが、ビットマップの背景とレイアウトが正しく描画されています。ただし、ボタンはタッチイベントに応答しません。

LinearLayoutをDialogクラスにロードする必要があると思いますが、ビットマップの上に描画するには、Viewクラスにロードする必要があると思います。

私は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);
             }
          });
4

1 に答える 1

1

サブビュークラスを追加して背景を描画する代わりに、次を追加するだけで済みました。

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

頑張っていたのかな!

于 2011-12-20T08:44:38.407 に答える