每次显示对话框时,子视图都会自动添加到另一个视图

时间:2011-01-09 09:43:40

标签: android

子视图已加入主视图。我不希望它如此。看截图。但是我希望对话框出现并且它消失了,对话框的视图没有加入到主视图中。在我调用对话框后,每次都会加入它的视图,它看起来像这样:

_http://s014.radikal.ru/i329/1101/cc/4ce75c9e8ae2.jpg

_http://i075.radikal.ru/1101/00/f1b132b07e25.jpg

但普通视图必须如下所示:

_http://i016.radikal.ru/1101/64/0b2a4718cd0e.jpg

为什么会这样?

dialogtable.xml

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Type here:"/>
    <EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/label"/>
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/entry"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"
        android:text="OK" />
    <Button
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/ok"
        android:layout_alignTop="@id/ok"
        android:text="Cancel" />
</RelativeLayout>

MainActivity的代码

@Override
protected Dialog onCreateDialog(int id) {
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.dialogtable, (ViewGroup)findViewById(R.id.LinearLayout01));

    AlertDialog.Builder alert = null;
    alert = new AlertDialog.Builder(this);
    alert.setView(layout);
    alert.setTitle("Create table");
    alert.setCancelable(false);
    alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {

        @Override
            public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    alert.setPositiveButton("OK",new DialogInterface.OnClickListener() {

        @Override
            public void onClick(DialogInterface dialog, int which) {

            Editable rows = fieldForRows.getText();
            Editable columns = fieldForColumns.getText();
            Editable border = fieldForWidthOfBroder.getText();


            TableAction tableAction = new TableAction(MainActivity.this.visualPane);
            tableAction.insertTable(rows, columns, border);
        }
    });
    Dialog dialog = null;
    dialog = new Dialog(this);
    switch (id) {
    case IDD_CUSTOM_INSERT_TABLE:
        dialog.setTitle("Insert table");
        dialog.setContentView(R.layout.dialogtable);
        return dialog;
    case IDD_CUSTOM_INSERT_IMAGE:
        dialog.setTitle("Insert image");
        return dialog;
    default:
        return alert.create();
    }
}

0 个答案:

没有答案