带有消息的 Android 自定义 ProgressDialog

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12115350/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:47:18  来源:igfitidea点击:

Android Custom ProgressDialog with Message

androidprogressdialog

提问by James Fazio

I have found a lot of tutorials on how to make a custom ProgressDialog without text. What is the easiest way to create a custom ProgressDialog with a custom image and a message. Something like this...

我找到了很多关于如何制作没有文本的自定义 ProgressDialog 的教程。使用自定义图像和消息创建自定义 ProgressDialog 的最简单方法是什么。像这样的东西...

enter image description here

在此处输入图片说明

回答by Frank Sposaro

Creating a Custom Dialog

创建自定义对话框

If you want a customized design for a dialog, you can create your own layout for the dialog window with layout and widget elements. After you've defined your layout, pass the root View object or layout resource ID to setContentView(View).

如果您想要一个对话框的自定义设计,您可以使用布局和小部件元素为对话框​​窗口创建自己的布局。定义布局后,将根视图对象或布局资源 ID 传递给 setContentView(View)。

For example, to create the dialog shown to the right:

例如,要创建右侧显示的对话框:

Create an XML layout saved as custom_dialog.xml:

创建一个保存为 custom_dialog.xml 的 XML 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dp"
              >
    <ImageView android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginRight="10dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />
</LinearLayout>

This XML defines an ImageView and a TextView inside a LinearLayout. Set the above layout as the dialog's content view and define the content for the ImageView and TextView elements:

这个 XML 在 LinearLayout 中定义了一个 ImageView 和一个 TextView。将上述布局设置为对话框的内容视图并定义 ImageView 和 TextView 元素的内容:

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");

TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

After you instantiate the Dialog, set your custom layout as the dialog's content view with setContentView(int), passing it the layout resource ID. Now that the Dialog has a defined layout, you can capture View objects from the layout with findViewById(int) and modify their content. That's it. You can now show the dialog as described in Showing A Dialog. A dialog made with the base Dialog class must have a title. If you don't call setTitle(), then the space used for the title remains empty, but still visible. If you don't want a title at all, then you should create your custom dialog using the AlertDialog class. However, because an AlertDialog is created easiest with the AlertDialog.Builder class, you do not have access to the setContentView(int) method used above. Instead, you must use setView(View). This method accepts a View object, so you need to inflate the layout's root View object from XML.

实例化对话框后,使用 setContentView(int) 将自定义布局设置为对话框的内容视图,并将布局资源 ID 传递给它。现在 Dialog 已经定义了布局,您可以使用 findViewById(int) 从布局中捕获 View 对象并修改它们的内容。就是这样。您现在可以按照显示对话框中的说明显示对话框。使用 Dialog 基类创建的对话框必须有一个标题。如果您不调用 setTitle(),则用于标题的空间仍为空,但仍然可见。如果您根本不需要标题,那么您应该使用 AlertDialog 类创建您的自定义对话框。但是,因为使用 AlertDialog.Builder 类最容易创建 AlertDialog,所以您无法访问上面使用的 setContentView(int) 方法。相反,您必须使用 setView(View)。

To inflate the XML layout, retrieve the LayoutInflater with getLayoutInflater() (or getSystemService()), and then call inflate(int, ViewGroup), where the first parameter is the layout resource ID and the second is the ID of the root View. At this point, you can use the inflated layout to find View objects in the layout and define the content for the ImageView and TextView elements. Then instantiate the AlertDialog.Builder and set the inflated layout for the dialog with setView(View).

要对 XML 布局进行膨胀,请使用 getLayoutInflater()(或 getSystemService())检索 LayoutInflater,然后调用 inflate(int, ViewGroup),其中第一个参数是布局资源 ID,第二个参数是根视图的 ID。此时,您可以使用膨胀的布局在布局中查找 View 对象并定义 ImageView 和 TextView 元素的内容。然后实例化 AlertDialog.Builder 并使用 setView(View) 设置对话框的膨胀布局。

Here's an example, creating a custom layout in an AlertDialog:

下面是在 AlertDialog 中创建自定义布局的示例:

AlertDialog.Builder builder;
AlertDialog alertDialog;

Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
                               (ViewGroup) findViewById(R.id.layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();

Using an AlertDialog for your custom layout lets you take advantage of built-in AlertDialog features like managed buttons, selectable lists, a title, an icon and so on.

为您的自定义布局使用 AlertDialog 可以让您利用内置的 AlertDialog 功能,如托管按钮、可选列表、标题、图标等。

For more information, refer to the reference documentation for the Dialog and AlertDialog.Builder classes.

有关更多信息,请参阅 Dialog 和 AlertDialog.Builder 类的参考文档。