Android 如何将一个活动显示为其他活动的弹出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3351553/
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
How to show an Activity as pop-up on other Activity?
提问by bhups
I have an Activity A, and there is a button B in the view. If somebody presses B then I want a pop-up which can take some part of screen making the A invisible in that area but rest of A is visible but not active. How can I achieve this?
我有一个活动 A,视图中有一个按钮 B。如果有人按下 B 那么我想要一个弹出窗口,它可以占据屏幕的一部分,使 A 在该区域不可见,但 A 的其余部分可见但不活动。我怎样才能做到这一点?
回答by oli
If you want to do this using an Activity instead of a Dialog, you can do this by setting the activity's theme to android:theme="@android:style/Theme.Dialog"
in the manifest - this will make the activity appear like a dialog (floating on top of whatever was underneath it).
如果您想使用 Activity 而不是 Dialog 来执行此操作,您可以通过android:theme="@android:style/Theme.Dialog"
在清单中将 Activity 的主题设置为 来执行此操作- 这将使 Activity 看起来像一个对话框(浮动在其下方的任何内容之上)。
回答by Actiwitty
For AppCompat, add
对于 AppCompat,添加
android:theme="@style/Theme.AppCompat.Dialog.Alert"
to the activity in AndroidManifest
到 AndroidManifest 中的活动
回答by Sephy
回答by Caio Faustino
Just to add on oli's answer, make sure to use the Dialog from the theme you are using in your application.
In my case I did android:theme="@android:style/Theme.Holo.Light.Dialog"
只是为了添加 oli 的答案,请确保使用您在应用程序中使用的主题中的 Dialog。
就我而言,我做到了android:theme="@android:style/Theme.Holo.Light.Dialog"
回答by Arslan Mehboob
For appcompat this can be used in the manifest
对于 appcompat 这可以在清单中使用
<activity android:theme="@style/Theme.Base.AppCompat.Dialog.FixedSize" >
</activity>
回答by Vikram Rao
Setting theme to android:theme="@android:style/android:Theme.Holo.Panel"
worked for me.
设置主题android:theme="@android:style/android:Theme.Holo.Panel"
为我工作。
Steps -
1. Set theme for the activity in manifest file to android:theme="@android:style/android:Theme.Holo.Panel"
(This has to be changed to whatever theme is being used). Ex:
步骤 - 1. 将清单文件中活动的主题设置为android:theme="@android:style/android:Theme.Holo.Panel"
(必须将其更改为正在使用的任何主题)。前任:
<activity
android:name=".EditActivity"
android:theme="@android:style/android:Theme.Holo.Panel"
android:label="@string/title_activity_edit" >
</activity>
- In the activity resource xml set appropriate padding and width on the root layout. I have set it to
0
and added a child layout at the beginning with alpha to show some part of the previous activity.
- 在活动资源 xml 中,在根布局上设置适当的填充和宽度。我已将其设置为
0
并在开头添加了一个带有 alpha 的子布局以显示先前活动的某些部分。
回答by Asad Mirza
if you are working with Material Designyou should use @android:style/Theme.Material.Dialog.NoActionBar
如果您正在使用Material Design,您应该使用@android:style/Theme.Material.Dialog.NoActionBar
回答by NN73 Player
You can do it programicly
您可以通过编程方式进行
Create Class MyDialog
创建类 MyDialog
import android.app.Activity;
import android.app.Dialog;
import android.view.Window;
import android.widget.TextView;
public class MyDialoge{
Activity activity;
TextView txt_Message;
Dialog dialog;
public ViewDialog(Activity activity) {
this.activity = activity;
}
public void showDialog(String message){
dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_progress_dialog);
txt_Message = dialog.findViewById(R.id.txt_message);
txt_Message.setText(message);
//if you want to dimiss the dialog
//dialog.dimiss()
dialog.show();
}
public void dimiss(){
try {
dialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
}
}
After that crate the layout -> call it my_dialog
然后将布局打包-> 将其称为 my_dialog
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello PopUp Message"/>
</RelativeLayout>
In your Activity
在您的活动中
MyDialog myDialog = new MyDialog(MainActivity.this);
myDialog.showDialog("Say Hello to Me");
To dimiss
关闭
myDialog.dimiss();