具有多个视图的自定义警报对话框 [英] Custom Alert Dialog with Multiple Views

查看:78
本文介绍了具有多个视图的自定义警报对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用下图所示的取消"按钮创建自定义提醒, 请帮助我创建这个.预先谢谢.!

I'm trying to create a custom alert with cancel button shown in the below Picture, Please help me to create this. Thanks in advance.!

推荐答案

MainActivity类

MainActivity class

    Button openAlert;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    openAlert = (Button)findViewById(R.id.openAlert);

    openAlert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
            View alertView = getLayoutInflater().inflate(R.layout.custom_alert, null);

            //Set the view
            alert.setView(alertView);
            //Show alert
            final AlertDialog alertDialog = alert.show();
            //Can not close the alert by touching outside.
            alertDialog.setCancelable(false);
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

            ImageView closeButton = (ImageView) alertView.findViewById(R.id.closeButton);

            closeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();
                }
            });



        }
    });



}

MainActivity XML->这只是一个打开警报的按钮

MainActivity XML -> This is just a button that opens the alert

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.vzw.www.multviewalert.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="OPEN ALERT"
        android:id="@+id/openAlert"/>

</RelativeLayout>

您需要为警报创建自定义布局

You need to create a custom layout for the alert

custom_alert.xml

custom_alert.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/alertContainer"
        android:background="@drawable/custom_alert_bg">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:id="@+id/rowOne">

            <ImageView
                android:id="@+id/rowOneButtonOne"
                android:background="#cdcdcd"
                android:layout_margin="4dp"
                android:layout_width="wrap_content"
                android:layout_height="100dp"
                android:layout_weight="0.33"
                />

            <ImageView
                android:id="@+id/rowOneButtonTwo"
                android:background="#cdcdcd"
                android:layout_margin="4dp"
                android:layout_width="wrap_content"
                android:layout_height="100dp"
                android:layout_weight="0.33"
                />

           <ImageView
                android:id="@+id/rowOneButtonThree"
                android:background="#cdcdcd"
                android:layout_margin="4dp"
                android:layout_width="wrap_content"
                android:layout_height="100dp"
                android:layout_weight="0.33"
                />
        </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/rowTwo"
        android:layout_below="@id/rowOne">

        <ImageView
            android:id="@+id/rowTwoButtonOne"
            android:background="#cdcdcd"
            android:layout_margin="4dp"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:layout_weight="0.33"
            />

        <ImageView
            android:id="@+id/rowTwoButtonTwo"
            android:background="#cdcdcd"
            android:layout_margin="4dp"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:layout_weight="0.33"
            />

        <ImageView
            android:id="@+id/rowTwoButtonThree"
            android:background="#cdcdcd"
            android:layout_margin="4dp"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:layout_weight="0.33"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/rowThree"
        android:layout_below="@id/rowTwo">

        <ImageView
            android:id="@+id/rowThreeButtonOne"
            android:background="#cdcdcd"
            android:layout_margin="4dp"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:layout_weight="0.33"
            />

        <ImageView
            android:id="@+id/rowThreeButtonTwo"
            android:background="#cdcdcd"
            android:layout_margin="4dp"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:layout_weight="0.33"
            />

        <ImageView
            android:id="@+id/rowThreeButtonThree"
            android:background="#cdcdcd"
            android:layout_margin="4dp"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:layout_weight="0.33"
            />
    </LinearLayout>

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</RelativeLayout>

<ImageView
    android:id="@+id/closeButton"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_weight="0.33"
    android:background="#cdcdcd" />


</RelativeLayout>

以下是警报背景的可绘制对象 custom_alert_bg.xml

Here is the drawable for the alert background custom_alert_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="#ffffff"/>
    <corners
        android:radius="5dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

这将产生以下内容

这是一个骨架....您必须在其中填充图像,并在每个单击侦听器上单击.

this is a skeleton.... you have to fill it in with your images and on click listeners for each.

此外,如果您想将顶部向下移动.您可以添加

Also, if you want to move the top section down. you can add

android:layout_marginTop="50dp"

到custom_alert.xml中的alertContainer ID

to the alertContainer ID in the custom_alert.xml

这篇关于具有多个视图的自定义警报对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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