Android Activity 作为对话框
我有一个名为 whereActity
的活动,它也有子对话框。现在,我想将此活动显示为另一个活动的对话框。
我怎样才能做到这一点?
I have an Activity named whereActity
which has child dialogs as well. Now, I want to display this activity as a dialog for another activity.
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
1 - 您可以动态地使用相同的活动作为对话框和全屏:
在调用
setContentView(...)
之前调用setTheme(android.R.style.Theme_Dialog)
和 Activity 中的super.oncreate()
。2 - 如果您不打算更改可以使用的活动主题样式
(如@faisal khan所述)
1 - You can use the same activity as both dialog and full screen, dynamically:
Call
setTheme(android.R.style.Theme_Dialog)
before callingsetContentView(...)
andsuper.oncreate()
in your Activity.2 - If you don't plan to change the activity theme style you can use
(as mentioned by @faisal khan)
如果您的 Activity 将呈现为对话框,只需将一个按钮添加到您的 Activity 的 xml 中,
然后在您的 Activity 的 Java 代码中附加一个单击侦听器。在侦听器中,只需调用 finish()
即可关闭对话框,返回到调用活动。
If your activity is being rendered as a dialog, simply add a button to your activity's xml,
Then attach a click listener in your Activity's Java code. In the listener, simply call
finish()
That should dismiss your dialog, returning you to the calling activity.
如果您想删除活动标题和为对话框提供自定义视图,将以下内容添加到清单的活动块中
,并使用所需的视图设计您的 Activity_layout
If you want to remove activity header & provide a custom view for the dialog add the following to the activity block of you manifest
and design your activity_layout with your desired view
将活动创建为对话框,这是完整示例
AndroidManife.xml
DialogActivity.kt
activity_dialog.xml
Create activity as dialog, Here is Full Example
AndroidManife.xml
<activity android:name=".appview.settings.view.DialogActivity" android:excludeFromRecents="true" android:theme="@style/Theme.AppCompat.Dialog"/>
DialogActivity.kt
activity_dialog.xml
在 Android 清单文件中设置主题。
并设置触摸时的对话框状态以完成。
Set the theme in your android manifest file.
And set the dialog state on touch to finish.
有时您可能会收到下面给出的异常
由以下原因引起:java.lang.IllegalStateException:您需要在此活动中使用 Theme.AppCompat 主题(或后代)。
因此,为了解决问题,您可以使用简单的解决方案
将您的活动主题添加到清单中作为 appCompact 的对话框。
它可能对某人有帮助。
Some times you can get the Exception which is given below
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
So for resolving you can use simple solution
add theme of you activity in manifest as dialog for appCompact.
It can be helpful for somebody.
要将 Activity 作为对话框启动,我在
AndroidManifest.xml
中将其定义为:在
activity
标记内使用此属性,以避免您的对话框出现在最近使用的应用程序列表中如果您当用户在对话框外部单击时,想要阻止您的对话框/活动被破坏:
在
Activity
使用setContentView()
之后:this.setFinishOnTouchOutside(false) );
现在,当我调用
startActivity()
时,它会显示为一个对话框,当用户按下后退按钮时,会显示上一个活动。请注意,如果您使用
ActionBarActivity
(或 AppCompat 主题),则需要使用@style/Theme.AppCompat.Dialog
代替。To start activity as dialog I defined it like this in
AndroidManifest.xml
:Use this property inside your
activity
tag to avoid that your Dialog appears in the recently used apps listIf you want to stop your dialog / activity from being destroyed when the user clicks outside of the dialog:
After
setContentView()
in yourActivity
use:this.setFinishOnTouchOutside(false);
Now when I call
startActivity()
it displays as a dialog, with the previous activity shown when the user presses the back button.Note that if you are using
ActionBarActivity
(or AppCompat theme), you'll need to use@style/Theme.AppCompat.Dialog
instead.使用此代码,以便当用户触摸对话框外部时对话框活动不会关闭:
需要 API 级别 11
Use this code so that the dialog activity won't be closed when the user touches outside the dialog box:
requires API level 11
您可以在values/styles.xml中定义此样式以执行更前面的Splash:
并使用它AndroidManifest.xml:
You can define this style in values/styles.xml to perform a more former Splash :
And use it AndroidManifest.xml:
如果您需要Appcompat版本
style.xml
yourmanifest.xml
If you need Appcompat Version
style.xml
yourmanifest.xml