Android Activity 作为对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1979369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Activity as a dialog
提问by d-man
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.
我有一个名为 Activity 的活动whereActity
,它也有子对话框。现在,我想将此活动显示为另一个活动的对话框。
How can I do that?
我怎样才能做到这一点?
回答by d-man
To start activity as dialog I defined it like this in AndroidManifest.xml
:
要将活动作为对话框开始,我在以下定义了它AndroidManifest.xml
:
<activity android:theme="@android:style/Theme.Dialog" />
Use this property inside your activity
tag to avoid that your Dialog appears in the recently used apps list
在activity
标签内使用此属性可避免您的 Dialog 出现在最近使用的应用程序列表中
android:excludeFromRecents="true"
If you want to stop your dialog / activity from being destroyed when the user clicks outside of the dialog:
如果您想在用户单击对话框外时阻止您的对话框/活动被破坏:
After setContentView()
in your Activity
use:
之后setContentView()
在您的Activity
使用:
this.setFinishOnTouchOutside(false);
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.
现在,当我调用startActivity()
它时,它显示为一个对话框,当用户按下后退按钮时会显示上一个活动。
Note that if you are using ActionBarActivity
(or AppCompat theme), you'll need to use @style/Theme.AppCompat.Dialog
instead.
请注意,如果您正在使用ActionBarActivity
(或 AppCompat 主题),则需要@style/Theme.AppCompat.Dialog
改用。
回答by Maverick
Use this code so that the dialog activity won't be closed when the user touches outside the dialog box:
使用此代码,以便在用户触摸对话框外时不会关闭对话框活动:
this.setFinishOnTouchOutside(false);
requires API level 11
需要 API 级别 11
回答by herbertD
You can define this style in values/styles.xml to perform a more former Splash :
您可以在 values/styles.xml 中定义此样式以执行更旧的 Splash :
<style name="Theme.UserDialog" parent="android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@drawable/trans</item>
</style>
And use it AndroidManifest.xml:
并使用它 AndroidManifest.xml:
<activity android:name=".SplashActivity"
android:configChanges="orientation"
android:screenOrientation="sensor"
android:theme="@style/Theme.UserDialog">
回答by M. Usman Khan
1 - You can use the same activity as both dialog and full screen, dynamically:
1 - 您可以动态地使用与对话框和全屏相同的活动:
Call setTheme(android.R.style.Theme_Dialog)
before calling setContentView(...)
and super.oncreate()
in your Activity.
呼叫setTheme(android.R.style.Theme_Dialog)
调用之前setContentView(...)
和super.oncreate()
你的活动。
2 - If you don't plan to change the activity theme style you can use
2 - 如果您不打算更改您可以使用的活动主题样式
<activity android:theme="@android:style/Theme.Dialog" />
(as mentioned by @faisal khan)
(如@faisal khan 所述)
回答by neferpitou
If you need Appcompat Version
如果您需要 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>
回答by aaronsnoswell
If your activity is being rendered as a dialog, simply add a button to your activity's xml,
如果您的活动被呈现为对话框,只需在您的活动的 xml 中添加一个按钮,
<Button
android:id="@+id/close_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dismiss" />
Then attach a click listener in your Activity's Java code. In the listener, simply call finish()
然后在您的 Activity 的 Java 代码中附加一个单击侦听器。在侦听器中,只需调用finish()
Button close_button = (Button) findViewById(R.id.close_button);
close_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
That should dismiss your dialog, returning you to the calling activity.
这应该会关闭您的对话,让您返回到调用活动。
回答by Ismail Iqbal
If you want to remove activity header & provide a custom view for the dialog add the following to the activity block of you manifest
如果要删除活动标题并为对话框提供自定义视图,请将以下内容添加到清单的活动块中
android:theme="@style/Base.Theme.AppCompat.Dialog"
and design your activity_layout with your desired view
并使用您想要的视图设计您的活动布局
回答by Gyan Swaroop Awasthi
Set the theme in your android manifest file.
在您的 android 清单文件中设置主题。
<activity android:name=".LoginActivity"
android:theme="@android:style/Theme.Dialog"/>
And set the dialog state on touch to finish.
并在触摸时设置对话框状态以完成。
this.setFinishOnTouchOutside(false);
回答by Sanjayrajsinh
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
class DialogActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_dialog) this.setFinishOnTouchOutside(true) btnOk.setOnClickListener { finish() } } }
activity_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#0072ff" android:gravity="center" android:orientation="vertical"> <LinearLayout android:layout_width="@dimen/_300sdp" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/txtTitle" style="@style/normal16Style" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingTop="20dp" android:paddingBottom="20dp" android:text="Download" android:textColorHint="#FFF" /> <View android:id="@+id/viewDivider" android:layout_width="match_parent" android:layout_height="2dp" android:background="#fff" android:backgroundTint="@color/white_90" app:layout_constraintBottom_toBottomOf="@id/txtTitle" /> <TextView style="@style/normal14Style" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingTop="20dp" android:paddingBottom="20dp" android:text="Your file is download" android:textColorHint="#FFF" /> <Button android:id="@+id/btnOk" style="@style/normal12Style" android:layout_width="100dp" android:layout_height="40dp" android:layout_marginBottom="20dp" android:background="@drawable/circle_corner_layout" android:text="Ok" android:textAllCaps="false" /> </LinearLayout> </LinearLayout>
AndroidManife.xml
<activity android:name=".appview.settings.view.DialogActivity" android:excludeFromRecents="true" android:theme="@style/Theme.AppCompat.Dialog"/>
对话活动.kt
class DialogActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_dialog) this.setFinishOnTouchOutside(true) btnOk.setOnClickListener { finish() } } }
活动对话框.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#0072ff" android:gravity="center" android:orientation="vertical"> <LinearLayout android:layout_width="@dimen/_300sdp" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/txtTitle" style="@style/normal16Style" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingTop="20dp" android:paddingBottom="20dp" android:text="Download" android:textColorHint="#FFF" /> <View android:id="@+id/viewDivider" android:layout_width="match_parent" android:layout_height="2dp" android:background="#fff" android:backgroundTint="@color/white_90" app:layout_constraintBottom_toBottomOf="@id/txtTitle" /> <TextView style="@style/normal14Style" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingTop="20dp" android:paddingBottom="20dp" android:text="Your file is download" android:textColorHint="#FFF" /> <Button android:id="@+id/btnOk" style="@style/normal12Style" android:layout_width="100dp" android:layout_height="40dp" android:layout_marginBottom="20dp" android:background="@drawable/circle_corner_layout" android:text="Ok" android:textAllCaps="false" /> </LinearLayout> </LinearLayout>
回答by Gyan Swaroop Awasthi
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.
引起:java.lang.IllegalStateException:您需要在此活动中使用 Theme.AppCompat 主题(或后代)。
So for resolving you can use simple solution
所以为了解决你可以使用简单的解决方案
add theme of you activity in manifest as dialog for appCompact.
在清单中添加您的活动主题作为 appCompact 的对话框。
android:theme="@style/Theme.AppCompat.Dialog"
It can be helpful for somebody.
它可能对某人有帮助。