Android自定义带消息的进度对话框

3

我发现了很多关于如何制作没有文本的自定义ProgressDialog的教程。创建一个带有自定义图像和消息的自定义ProgressDialog的最简单方法是什么?就像这样...

enter image description here

1个回答

21

创建自定义对话框

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

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

创建一个名为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>

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

在实例化Dialog后,使用setContentView(int)方法将自定义布局设置为对话框的内容视图,传递布局资源ID。现在对话框具有了定义的布局,您可以使用findViewById(int)从布局中捕获View对象并修改其内容。 就是这样。您现在可以按照“显示对话框”中所述的方式显示对话框。 使用基本的Dialog类制作的对话框必须具有标题。如果您不调用setTitle()方法,则用于标题的空间保持为空,但仍然可见。如果您根本不想要标题,则应使用AlertDialog类创建自定义对话框。但是,由于最容易使用AlertDialog.Builder类创建AlertDialog,因此您无法访问上述使用的setContentView(int)方法。相反,您必须使用setView(View)方法。该方法接受一个View对象,因此需要从XML中膨胀布局的根View对象。
要膨胀XML布局,请使用getLayoutInflater()(或getSystemService())检索LayoutInflater,然后调用inflate(int, ViewGroup)方法,其中第一个参数是布局资源ID,第二个参数是根View的ID。此时,您可以使用膨胀的布局在布局中查找View对象,并为ImageView和TextView元素定义内容。然后实例化AlertDialog.Builder并使用setView(View)方法将膨胀的布局设置为对话框。
以下是在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功能,如管理按钮、可选择的列表、标题、图标等。

如需了解更多信息,请参考Dialog和AlertDialog.Builder类的参考文档。


6
谢谢您替我Google一下,我会去角落里坐几分钟以示惩罚。 - James Fazio

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