Android中的对话框和弹出窗口 [英] Dialogs and Popups in Android
问题描述
http://developer.android.com/中的Android设计文档design/building-blocks/dialogs.html 明确区分了对话框,警报,弹出窗口和吐司.它还建议通过DialogFragment
类实现对话框,并通过Toast
类实现 Toasts .但是我不清楚 Popups 是应该使用PopupWindow
还是DialogFragment
来实现.
The Android design documentation in http://developer.android.com/design/building-blocks/dialogs.html makes a clear differentiation between Dialogs, Alerts, Popups and Toasts. It also recommends the implementation of Dialogs by means of the DialogFragment
class and Toasts by means of the Toast
class. However it's not clear to me whether Popups should be implemented with PopupWindow
or with DialogFragment
.
我知道DialogFragments
通常带有确定/取消"按钮,并且PopupWindows
的位置可以定义,但是:
I know that DialogFragments
usually come with Ok/Cancel buttons and that the location of PopupWindows
can be defined, but:
- 这些细微差别是使用一个或另一个的唯一论点吗?
-
DialogFragment
是PopupWindow
的后继者,在某个时候会被弃用吗? - 根据 https://stackoverflow.com/a/15165554/2482894 中的答案,PopupWindow为有限到一些模板",但是我在类文档中找不到对有限模板的任何引用.
- 因此,最后,您将如何实现类似 http:/的弹出窗口/developer.android.com/design/media/dialogs_popups_example.png 以及为什么?
- Are these slight differences the only arguments to use one or the other?
- Is
DialogFragment
the successor ofPopupWindow
that will be deprecated at some point? - According to the answer in https://stackoverflow.com/a/15165554/2482894, PopupWindow is "Limited to a few templates", but I can't find any reference to a limited amount of templates in the class documentation.
- So, finally, how would you implement Popups like these http://developer.android.com/design/media/dialogs_popups_example.png and why?
推荐答案
如果您想要对话框中显示的对话框,只需通过如下所述创建自定义对话框即可:
If you want dialog as shown in the link, just make them by making custom dialog as mentioned below:
制作一个对话框对象:
Dialog dialog = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);
为此对话框设置自定义视图:
Set custom view to this dialog:
show_dialog(){
dialog.setContentView(R.layout.custom_dialog);//your custom dialog layout.
}
您的自定义布局应如下所示:
Your custom layout should be like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/custom_dialog_first_rl"
android:background="@android:color/black">
<!-- write code for rest of your UI here -->
</RelativeLayout>
现在像这样在show_dialog()中为您的第一个相对布局设置Alpha:
Now set alpha for your first relative layout in show_dialog() like this:
show_dialog(){
dialog.setContentView(R.layout.custom_dialog);//your custom dialog layout.
RelativeLayout custom_dialog_first_rl=(RelativeLayout)dialog.findViewById(R.id.custom_dialog_first_rl);
custom_dialog_first_rl.getBackground().setAlpha(170);
}
在要显示此对话框的地方呼叫show_dialog()
Call show_dialog()
where you wanna show this dialog
这篇关于Android中的对话框和弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!