AlertDialog不包含我的内容

时间:2017-05-17 15:01:34

标签: android layout alertdialog

我正在使用警告对话框显示带微调器的视图,以设置图表的周期和比例

但它没有包装我的内容和我试过的所有tuto都失败了......

我的代码

        LinearLayout vue = new LinearLayout(context);
        vue.setOrientation(LinearLayout.VERTICAL);
        vue.addView(diag.getView());

        choixPeriode = (Spinner) vue.findViewById(R.id.spinner_periode);
        choixEchelle = (Spinner) vue.findViewById(R.id.spinner_echelle);
        if(choixEchelle == null)
            choixEchelle = new Spinner(context);
        final int oldPeriode = DiagramController.PeriodeToPos(this.periode);
        final int oldEchelle = DiagramController.echelleToPos(this.echelle);
        choixPeriode.setSelection(oldPeriode);
        choixEchelle.setSelection(oldEchelle);


        LinearLayout containerButton = new LinearLayout(context);
        containerButton.setGravity(Gravity.CENTER);

        final ImageView croix = ((ImageView) vue.findViewById(R.id.croix));
        final Button ok = new Button(context);
        ok.setText(R.string.valider);
        final Button cancel = new Button(context);
        cancel.setText(android.R.string.cancel);

        containerButton.addView(ok);
        containerButton.addView(cancel);

        vue.addView(containerButton);

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setView(vue);

        final AlertDialog alertDialog = dialogBuilder.create();
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(alertDialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        alertDialog.getWindow().setAttributes(lp);
        alertDialog.show();

        croix.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                periode = DiagramController.posToPeriode(choixPeriode.getSelectedItemPosition());
                echelle = DiagramController.posToEchelle(choixEchelle.getSelectedItemPosition());
                changePeriode();
                alertDialog.dismiss();
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });

diag.getView()返回一个带有标题,微调器和图表的LinearLayout。

以人像结果 Result in portrait

横向结果 Result in landscape

自定义对话框的结果

enter image description here

1 个答案:

答案 0 :(得分:1)

1。)您需要创建自定义xml布局。

2.)创建一个新类。并在onCreate中编写代码。

public class MyDialog extends Dialog {

private TextView textView;

private Context context;

public MyDialog(Context context) {
    super(context);

    this.context = context;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.my_layout);

    //Your code
    textView = (TextView) findViewById(R.id.textView);


}
}