使用自定义 xml 视图在 Android 中创建 AlertDialog

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

Create a AlertDialog in Android with custom xml view

androidxmlandroid-alertdialogandroid-custom-view

提问by Quinn Wei

enter image description here

在此处输入图片说明

How to create a dialog like this in Android? My idea is to create a custom xml view and inflate it into the dialog. any other suggestions? and what is the "drag line" at the middle of the dialog called? I really need that.

如何在Android中创建这样的对话框?我的想法是创建一个自定义 xml 视图并将其膨胀到对话框中。还有其他建议吗?对话框中间的“拖动线”是什么?我真的需要那个。

Thanks in advance

提前致谢

回答by Pratik Dasa

Try below code for your reference:

尝试以下代码供您参考:

main.xml

主文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonShowCustomDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Custom Dialog" />

</LinearLayout>

dialog.xml: Make design whatever you want in dialog.

dialog.xml:在对话框中随心所欲地进行设计。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF" 
        android:layout_toRightOf="@+id/image"/>/>

     <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/image"
        />

</RelativeLayout>

MainActivity:

主要活动:

public class MainActivity extends Activity {

    final Context context = this;
    private Button button;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.buttonShowCustomDialog);

        // add button listener
        button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {

            // custom dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.dialog);
            dialog.setTitle("Title...");

            // set the custom dialog components - text, image and button
            TextView text = (TextView) dialog.findViewById(R.id.text);
            text.setText("Android custom dialog example!");
            ImageView image = (ImageView) dialog.findViewById(R.id.image);
            image.setImageResource(R.drawable.ic_launcher);

            Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            dialog.show();
          }
        });
    }
}

回答by M D

Create Alert Dialogand Inflate Custom Layoutand set layout to your Alert Dialog.

创建Alert DialogInflate Custom Layout设置布局到您的Alert Dialog.

Here is the link for some demo examples for reference

这是一些演示示例的链接以供参考

and drag line is called as Progress Bar/Seek Barin android. You can add this into your layout and handle it's progress.

和拖动线Progress Bar/Seek Bar在android中被调用。您可以将其添加到您的布局中并处理它的进度。

回答by Ajit Pratap Singh

I think this is the standard dialog for android if you are using the Holo theme. You just need to use the holo theme for your application and it should work.

如果您使用 Holo 主题,我认为这是 android 的标准对话框。您只需要为您的应用程序使用全息主题,它应该可以工作。

Add this to your application tag in your manifest file android:theme="@android:style/Theme.Holo"

将此添加到清单文件中的应用程序标签 android:theme="@android:style/Theme.Holo"