android:对话框:使用布局

一,代码:

xml:

<?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="match_parent"
    android:background="#eeeeee"
    android:orientation="horizontal">

    <ImageView
        android:src="@drawable/b"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

    <TextView
        android:text="Android"
        android:gravity="center_vertical"
        android:layout_marginStart="30dp"
        android:layout_width="wrap_content"
        android:layout_height="100dp"/>

</LinearLayout>

java:

        //给按钮增加点击事件
        Button button3 = findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View dialog_view=getLayoutInflater().inflate(R.layout.dialog_view,null);

                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.a)
                        .setTitle("对话框")
                        .setMessage("Hello")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Log.e("btnclick","点击确认");
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Log.e("btnclick","点击取消");
                            }
                        })
                        .setNeutralButton("其他功能", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Log.e("btnclick","点击中性");
                            }
                        })
                        .setView(dialog_view)
                        .create()
                        .show();

             }
        });

二,测试效果:

posted @ 2025-05-10 09:02  刘宏缔的架构森林  阅读(8)  评论(0)    收藏  举报