Android中的对话框和弹出窗口

leo*_*o9r 12 android popup popupwindow dialogfragment

http://developer.android.com/design/building-blocks/dialogs.html中的Android设计文档明确区分了对话框,警报,弹出窗口和Toast.它还建议通过类和Toasts通过类来实现Dialogs.但是,我不清楚是否应该使用或使用弹出窗口.DialogFragmentToastPopupWindowDialogFragment

我知道DialogFragments通常带有Ok/Cancel按钮,并且PopupWindows可以定义位置,但是:

小智 2

如果您想要链接中所示的对话框,只需通过创建自定义对话框来制作它们,如下所述:

创建一个对话框对象:

Dialog dialog = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);
Run Code Online (Sandbox Code Playgroud)

为此对话框设置自定义视图:

show_dialog(){
    dialog.setContentView(R.layout.custom_dialog);//your custom dialog layout.
}
Run Code Online (Sandbox Code Playgroud)

您的自定义布局应该是这样的:

<?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>
Run Code Online (Sandbox Code Playgroud)

现在在 show_dialog() 中为第一个相对布局设置 alpha,如下所示:

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);
}
Run Code Online (Sandbox Code Playgroud)

调用show_dialog()您想要显示此对话框的位置