前言
我们平时在网上可以看到很多新建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>
<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);
接下来就是写监听代码了,这个大家都会,就不再赘述了总结
我也是个新手,在实习的过程中遇到了各种问题,所以想把它写下来,方便自己以后查阅,也希望可以帮到需要的人