android自定义AlertDialog背景、背景边框消除、自定义背景按钮监听事件

前言

我们平时在网上可以看到很多新建AlertDialog的介绍,在这里就不再说了,我主要要介绍一下对于自定义AlertDialog的背景的使用,以及如何给背景按钮设置监听事件,如何消除边框

一、如何使用自定义AlertDialog背景

在这里我新建了一个layout文件,命名为activity_out
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<RelativeLayout
	    android:id="@+id/back"
	    android:layout_width="280dp"
	    android:layout_height="240dp"
	    android:layout_centerHorizontal="true"
	    
	    android:layout_centerVertical="true"
	    android:background="@drawable/tsk2" >

	<ImageView
	    android:id="@+id/warning"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_alignParentTop="true"
	    android:layout_centerHorizontal="true"
	    android:layout_marginTop="36dp"
	    android:background="@drawable/tszt" />

	<ImageView
	    android:id="@+id/out_dialog"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@+id/warning"
	    android:layout_centerHorizontal="true"
	    android:layout_marginTop="36dp"
	    android:background="@drawable/sfqdtczt" />

	<Button
	    android:id="@+id/yes"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@+id/out_dialog"
	    android:layout_marginTop="43dp"
	    android:layout_toLeftOf="@+id/warning"
	    android:background="@drawable/yes_click" />

	<Button
	    android:id="@+id/no"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_alignBaseline="@+id/yes"
	    android:layout_alignBottom="@+id/yes"
	    android:layout_toRightOf="@+id/warning"
	    android:background="@drawable/no_click" />

</RelativeLayout>

接着就要去使用它了,在activity中,新建一个alertdialog,然后进行绑定
//使用自定义背景,
				View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_out,null);
				 builder.setView(view);
<span style="white-space:pre">				</span>builder.show();
这个时候你会发现,在这个图片背景下面会有系统alertdialog的边框出现,所以我们要修改steView();
//消除背景与系统dialog的边框
				((AlertDialog) builder).setView(view,0,0,0,0);
				builder.show();
这样就可以解决这个使用自定义背景啦
二、如何给alertdialog的按钮设置监听事件
网上也有很多类似的方法介绍,我自己使用的时候发现大多都是一样的,但是效果却不好,会引发程序闪退,最终我用了这个方法,得到了解决
//设置按钮监听
				Window window=builder.getWindow();
				window.setContentView(R.layout.activity_out);
				 Button yes=(Button)window.findViewById(R.id.yes);
				 Button no=(Button)window.findViewById(R.id.no);
接下来就是写监听代码了,这个大家都会,就不再赘述了
总结
我也是个新手,在实习的过程中遇到了各种问题,所以想把它写下来,方便自己以后查阅,也希望可以帮到需要的人
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值