Android Activity as a dialog

匿名 (未验证) 提交于 2019-12-03 02:08:02

问题:

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?

回答1:

To start activity as dialog I defined it like this in 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

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:

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.



回答2:

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



回答3:

You can define this style in values/styles.xml to perform a more former 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:

   <activity android:name=".SplashActivity"           android:configChanges="orientation"           android:screenOrientation="sensor"           android:theme="@style/Theme.UserDialog"> 


回答4:

1 - You can use the same activity as both dialog and full screen, dynamically:

Call setTheme(android.R.style.Theme_Dialog) before calling setContentView(...) and super.oncreate() in your Activity.

2 - If you don't plan to change the activity theme style you can use

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

(as mentioned by @faisal khan)



回答5:

If your activity is being rendered as a dialog, simply add a button to your activity's 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()

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.



回答6:

If you need Appcompat Version

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> 


回答7:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!