关闭之前的布局并膨胀另一个

时间:2012-04-25 08:59:53

标签: android layout

我有两个布局,我一个接一个地膨胀它,我想要忽略以前的视图并给另一个充气,但视图转移到背景,因此两个对话框都存在。我想要关闭对话框在后台。
我试过以下修复:

1.dialog.dismiss(); ---不工作

2.view.setVisibility(View.GONE); ---对话框的标题仍然与对话框一起保留。

我附上了整个代码。请让我知道如何解决这个问题。谢谢。

menuproject.java

        package com.menuproject;

        import android.app.Activity;
        import android.app.AlertDialog;
        import android.os.Bundle;
        import android.view.LayoutInflater;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;

        public class menuproject extends Activity {
            /** Called when the activity is first created. */

            AlertDialog.Builder dialog;
            AlertDialog.Builder dialog2;
            private AlertDialog alert = null;
            private AlertDialog alert2 = null;
            View updatedialog;
            View updatedialog2;

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

            }

            protected void layoutsecond() {
                // TODO Auto-generated method stub
                this.dialog2 = new AlertDialog.Builder(this);
                this.dialog2.setTitle("Update");

                LayoutInflater layoutInflater = LayoutInflater.from(this);
                updatedialog2 = layoutInflater.inflate(R.layout.updatedialog2, null);

                this.dialog2.setView(updatedialog2);
                alert2 = this.dialog2.create();
                this.dialog2.show();

            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // TODO Auto-generated method stub
                MenuItem mi = menu.add(1, 1, 1, "My Menu1");
                mi.setIcon(getResources().getDrawable(R.drawable.icon));

                return super.onCreateOptionsMenu(menu);
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {

                switch (item.getItemId()) {
                case 1:

                    layoutfirst();

                    Button b = (Button) updatedialog.findViewById(R.id.Button11);
                    b.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {
                            // TODO Auto-generated method stub

                            // updatedialog.setVisibility(View.GONE);

                            // alert.dismiss();

                            layoutsecond();

                        }
                    });

                    break;

                }
                return super.onOptionsItemSelected(item);
            }

            private void layoutfirst() {
                // TODO Auto-generated method stub

                this.dialog = new AlertDialog.Builder(this);
                this.dialog.setTitle("Update");
                // this.dialog.setMessage("");

                LayoutInflater layoutInflater = LayoutInflater.from(this);
                updatedialog = layoutInflater.inflate(R.layout.updatedialog, null);

                this.dialog.setView(updatedialog);
                alert = this.dialog.create();
                this.dialog.show();

            }

        }

main.xml中

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    </LinearLayout>

updatedialog.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ScrollView02" android:layout_width="fill_parent" 
        android:layout_height="fill_parent" android:fillViewport="true">

<LinearLayout
    android:id="@+id/liner1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >
        <requestFocus></requestFocus>

        <Button
            android:id="@+id/Button11"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableLeft="@android:drawable/ic_menu_call"
            android:text="Update Tel" />

        <Button
            android:id="@+id/Button10"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableLeft="@android:drawable/ic_menu_call"
            android:text="Update Address" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

    </View>

</LinearLayout>
</ScrollView>

updatedialog2.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ScrollView021" android:layout_width="fill_parent" 
        android:layout_height="fill_parent" android:fillViewport="true">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >
        <requestFocus></requestFocus>

        <Button
            android:id="@+id/Button01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableLeft="@android:drawable/ic_menu_call"
            android:text="Update Mobile" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

    </View>

</LinearLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:0)

您是否尝试过alert.setContentView(inflated_layout)而不是dialog.setView(view_name)