Android的定制ProgressDialog与信息 [英] Android Custom ProgressDialog with Message

查看:195
本文介绍了Android的定制ProgressDialog与信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了很多关于如何制作一个自定义的ProgressDialog没有文字教程。什么是创建一个自定义ProgressDialog具有自定义图像和信息的最简单方法。事情是这样的......

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...

推荐答案

创建自定义对话框

如果你想有一个自定义设计一个对话框,您可以创建自己的布局的对话框窗口布局和窗口小部件的元素。当你定义你的布局,通过根视图对象或者布局资源ID来的setContentView(查看)。

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).

例如,创建如右图所示的对话框:

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

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

Create an XML layout saved as custom_dialog.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>

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

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:

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);

在实例化对话框,设置您的自定义布局对话框的使用的setContentView(int)的内容来看,它传递的布局资源ID。现在,该对话框中有一个定义的布局,可以捕获与findViewById(int)的布局视图对象并修改其内容。 而已。现在,您可以显示对话框,以显示对话框描述。 与基对话框类制成的对话框必须有一个标题。如果你不叫的setTitle(),然后用于标题的空间保持为空,但仍然可见。如果你根本不想要一个标题,那么你应该创建一个使用AlertDialog类的自定义对话框。但是,因为一个AlertDialog创建最容易与AlertDialog.Builder类,则没有获得上述所用的的setContentView(int)方法。相反,你必须使用的setView(查看)。此方法接受一个视图对象,所以你需要从充气XML布局的根查看对象。

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.

要膨胀的XML布局,检索与getLayoutInflater()(或getSystemService())的LayoutInflater,然后调用inflate(INT,ViewGroup中),其中第一个参数是布局资源ID,第二个是的标识根查看。在这一点上,你可以用充气布局,以找到在布局视图对象,并定义了ImageView的和的TextView元素的含量。然后实例化AlertDialog.Builder并设置充气布局具有的setView(视图)对话框。

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).

下面是一个例子,在AlertDialog创建自定义布局:

Here's an example, creating a custom layout in an 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();

使用AlertDialog您的自定义布局,您可以利用内置式AlertDialog功能,如管理按钮,选择列表,一个标题,一个图标等。

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.Builder类的参考文档。

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

这篇关于Android的定制ProgressDialog与信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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