首页 > 编程技术 > android

Android自定义弹窗提示效果

发布时间:2021-9-12 16:00

本文实例为大家分享了Android 自定义弹窗提示的具体代码,供大家参考,具体内容如下

Java文件:

private void showSetDeBugDialog() {
        AlertDialog.Builder setDeBugDialog = new AlertDialog.Builder(this);
        //获取界面
        View dialogView = LayoutInflater.from(this).inflate(R.layout.system_admin_psw_alert_dialog, null);
        //将界面填充到AlertDiaLog容器并去除边框
        setDeBugDialog.setView(dialogView,0,0,0,0);
        //初始化控件
        TextView but_cancel = dialogView.findViewById(R.id.but_cancel);
        TextView but_confirm = dialogView.findViewById(R.id.but_confirm);
        //取消点击外部消失弹窗
        setDeBugDialog.setCancelable(false);
        //创建AlertDiaLog
        setDeBugDialog.create();
        //AlertDiaLog显示
        final AlertDialog customAlert = setDeBugDialog.show();
        //设置AlertDiaLog宽高属性
//        WindowManager.LayoutParams params = Objects.requireNonNull(customAlert.getWindow()).getAttributes();
//        params.width = 200;
//        params.height = 200 ;
//        customAlert.getWindow().setAttributes(params);
        // 移除dialog的decorview背景色
        Objects.requireNonNull(customAlert.getWindow()).getDecorView().setBackground(null);
        //设置自定义界面的点击事件逻辑
        but_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                customAlert.dismiss();
            }
        });
        but_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                customAlert.dismiss();
            }
        });
    }

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/fillet_fill_stroke">

    <ImageView
        android:layout_width= "38dp"
        android:layout_height="38dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:src="@mipmap/wenti"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="20dp"
        android:paddingBottom="20dp"
        android:textColor="#5C5C5C"
        android:textSize="20sp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:text="确定要退出吗?"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#eee"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/but_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="#999"
            android:paddingTop="15dp"
            android:paddingBottom="15dp"
            android:text="取消"/>

        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#eee"/>

        <TextView
            android:id="@+id/but_confirm"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:paddingTop="15dp"
            android:paddingBottom="15dp"
            android:textColor="#5C5C5C"
            android:textStyle="bold"
            android:text="确定"/>

    </LinearLayout>

</LinearLayout>

资源文件:

背景样式

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

    <!--描边设置-->
    <stroke android:color="@android:color/darker_gray"
        android:width="1px" />

    <!--填充设置-->
    <solid android:color="@android:color/white"/>

    <!--圆角设置-->
    <corners android:radius="15dp"/>

</shape>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

标签:[!--infotagslink--]

您可能感兴趣的文章: