Android中的对话框和弹出窗口 [英] Dialogs and Popups in Android

查看:82
本文介绍了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:

  • Are these slight differences the only arguments to use one or the other?
  • Is DialogFragment the successor of PopupWindow 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆