Android中的对话框和弹出窗口

12
http://developer.android.com/design/building-blocks/dialogs.html的Android设计文档中,明确区分了对话框、警告、弹出窗口和吐司。它还推荐使用DialogFragment类实现对话框和使用Toast类实现吐司。然而,我不确定弹出窗口应该使用PopupWindow还是DialogFragment实现。
我知道DialogFragment通常带有“确定/取消”按钮,而PopupWindow的位置可以定义,但是:

根据dialogs.html的建议,即使是像list_dialog这样没有按钮的窗口,他们也推荐使用DialogFragment。此外,我认为基于Fragments的事实使它更容易添加到BackStack中,而我不清楚如何使用PopupWindow实现这一点。因此,我采用了DialogFragment方法,尽管我仍然希望对比两个类的更深入的细节。 - leo9r
1个回答

2

如果您想要像链接中显示的对话框,只需按照以下方法制作自定义对话框:

创建一个对话框对象:

Dialog dialog = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);

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

show_dialog(){
    dialog.setContentView(R.layout.custom_dialog);//your custom dialog layout.
}

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

您的自定义布局应该如下所示:

<?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,如下所示:
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()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接