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

我该怎么做呢?


当前回答

如果你需要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>

其他回答

如果你需要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>

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

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

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

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

(正如@faisal khan所提到的)

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

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

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

有时候你可以得到下面给出的异常

导致:java.lang.IllegalStateException:你需要使用一个主题。AppCompat主题(或后代)。

所以你可以用简单的方法来解决

在manifest中为appCompact添加活动主题。

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

它对某些人是有帮助的。

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

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

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

this.setFinishOnTouchOutside(false);