带消息的Android Custom ProgressDialog

Jam*_*zio 3 android progressdialog

我发现了很多关于如何在没有文本的情况下制作自定义ProgressDialog的教程.使用自定义图像和消息创建自定义ProgressDialog的最简单方法是什么.像这样......

在此输入图像描述

Fra*_*aro 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>
Run Code Online (Sandbox Code Playgroud)

此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);
Run Code Online (Sandbox Code Playgroud)

实例化对话框后,使用setContentView(int)将自定义布局设置为对话框的内容视图,并将布局资源ID传递给它.既然Dialog具有已定义的布局,您可以使用findViewById(int)从布局中捕获View对象并修改其内容.而已.您现在可以按显示对话框中的说明显示对话框.使用基本Dialog类创建的对话框必须具有标题.如果不调用setTitle(),则用于标题的空间仍为空,但仍然可见.如果您根本不需要标题,则应使用AlertDialog类创建自定义对话框.但是,因为使用AlertDialog.Builder类创建的AlertDialog最简单,所以您无权访问上面使用的setContentView(int)方法.相反,您必须使用setView(View).此方法接受View对象,因此您需要从XML中扩展布局的根View对象.

以膨胀XML布局,检索与getLayoutInflater(所述LayoutInflater)(或getSystemService()),然后调用膨胀(INT,ViewGroup中),其中第一个参数是布局资源ID和所述第二是根查看的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();
Run Code Online (Sandbox Code Playgroud)

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

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

  • 感谢你为我搜索,我会坐在角落里待几分钟. (4认同)