我有一个名为whereactivity的活动,它也有子对话框。现在,我想将这个活动显示为另一个活动的对话框。

我该怎么做呢?


当前回答

使用下面的代码,当用户在对话框外触摸时,对话框活动不会关闭:

this.setFinishOnTouchOutside(false);

要求API等级11

其他回答

1 -你可以动态地使用与对话框和全屏相同的活动:

在Activity中调用setContentView(…)和super.oncreate()之前调用setTheme(android.R.style.Theme_Dialog)。

2 -如果你不打算改变活动主题风格,你可以使用

<activity android:theme="@android:style/Theme.Dialog" />

(正如@faisal khan所提到的)

如果你的活动被渲染成一个对话框,只需在你的活动的xml中添加一个按钮,

<Button
    android:id="@+id/close_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Dismiss" />

然后在Activity的Java代码中附加一个单击侦听器。在侦听器中,只需调用finish()

Button close_button = (Button) findViewById(R.id.close_button);
close_button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        finish();
    }
});

这应该会终止对话框,返回到调用活动。

如果你需要Appcompat版本

style.xml

    <!-- Base application theme. -->
    <style name="AppDialogTheme" parent="Theme.AppCompat.Light.Dialog">
        <!-- Customize your theme here. -->
        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>

yourmanifest.xml

    <activity
          android:name=".MyActivity"
          android:label="@string/title"
          android:theme="@style/AppDialogTheme">
    </activity>

如果你想删除活动标题并为对话框提供一个自定义视图,请将以下内容添加到你清单的活动块中

android:theme="@style/Base.Theme.AppCompat.Dialog"

用你想要的视图设计你的activity_layout

在你的android manifest文件中设置主题。

<activity android:name=".LoginActivity"
            android:theme="@android:style/Theme.Dialog"/>

并将对话框状态设置为触摸完成。

this.setFinishOnTouchOutside(false);