使用自定义视图的AlertDialog

来源:互联网 发布:惠州淘宝培训班 编辑:程序博客网 时间:2024/05/01 03:10

使用自定义视图的AlertDialog主要分为以下几个步骤:

1)利用XML文件构建自己的的视图

2)将视图添加到AlertDialog中

* 在进行第二步之前,有时需要对对话框窗口进行额外的设置

下面分步骤叙述:

1)写XML文件

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="match_parent"    android:layout_height="match_parent"   android:layout_marginLeft="5dp"   android:layout_marginRight="5dp"   android:background="@drawable/buytip_bg" >    <TextView       android:id="@+id/textView1"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:layout_centerHorizontal="true"       android:gravity="center"       android:text="提示"       android:textColor="#ff000000"       android:textSize="24sp"       android:textStyle="bold" />    <TextView       android:id="@+id/tv_dialogview_message"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:layout_centerHorizontal="true"       android:gravity="center"       android:text="正文信息"       android:layout_centerInParent="true"       android:textColor="#ff000000"       android:textSize="24sp"       android:textStyle="bold"       />   <Button       android:id="@+id/btn_dialogview_cancel"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:layout_alignParentLeft="true"       android:layout_marginLeft="10dp"       android:background="@drawable/cancel_selector"       android:layout_alignParentBottom="true"       android:layout_marginBottom="18dp"       />    <Button        android:id="@+id/btn_dialogview_ok"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:layout_alignParentRight="true"       android:layout_marginRight="10dp"       android:background="@drawable/ok_selector"      android:layout_alignParentBottom="true"       android:layout_marginBottom="18dp"       /></RelativeLayout>


这是一个很简单的布局文件,有几个细节注意一下

i. 容器的宽高设置的均为match_parent,容器设置了左右外边距

ii.两个button均使用了wrap_content来完整呈现各自的背景图片,背景图片不是.9图

2)将视图添加到AlertDialog中

首先用View的inflate方法或者LayoutInflater方法将xml膨胀为View对象。在添加自定义视图的时候可以使用如下方法:setContentView或者setView方法,这两者的区别是:

对话框也是一个视图,它是画在一个Window对象上的。

默认的对话框总体分为3个部分将window填满,即标题区域视图,内容区域视图和按钮区域视图。如果不改变视图的样式只改变视图中呈现的内容的话,改变标题区域视图的文字内容使用setTitle方法,改变内容区域视图的文字内容使用setMesssage方法。如果要改变视图本身的话,改变标题区域视图使用setCustomTitle(view)方法,用参数中的view替代默认的标题区域视图,改变内容区域视图使用setView(view)方法,用参数中的view替代默认的内容区域视图。如果不设置setTitle/setCustomTitle以及setXXXButton,即让标题区域和按钮区域内容都为空,仅仅使用setView(view)的时候,默认内容区域视图周围会有一圈黑边,去掉黑边的方式有两种:

1)利用style或者setInverseBackgroundForced方法让AlertDialog的背景色从默认的黑色变为透明;

2)使用setView的一个5参数重载方法,方法签名如下:

setView(View view, int viewSpacingLeft,int viewSpacingTop, int viewSpacingRight, int viewSpacingBottom)


后四个参数的作用就是用来显式的声明从view代表的内容区域空间向上下左右再延伸出多少空间,延伸出去的空间也是内容区域的一部分,因为没有视图覆盖就露出了低下背景色黑色,所以这里只要显式的设定这四个方向的延伸区域均为0即可

setView(view,0,0,0,0)

作为内容区域的一部分,通过view的findViewById,可以获得视图中各个子视图的引用,进而设置内容或者监听器。利用setView方法将对话框的内容区域设置好后,就可以调用show方法来显示拥有自定义内容区域的对话框了。

setView方法是AlertDialog的自有方法,而setContentView方法是AlertDialog继承自Dialog的方法。setContentView方法会将膨胀后的视图作为整个装载对话框的Window的视图。所以,尽管使用setView和setContentView表面上看起来差不多,但是实际上层次不一样。因为setContentView(view)是将view放到window上,所以必须得先有window对象才可以,而window对象是在AlertDialog调用了show方法之后才会有的。所以setContentView方法必须在show方法调用之后才可以调用,否则会产生异常。setContentView将参数view对象添加到window上,进而通过window的findViewById方法来获得view中的各个子视图的引用,进而设置内容或者监听器。

当使用setContentView的时候,如果不指定任何额外的参数,那么会对话框会按照xml文件的原样来进行显示。只不过作为对话框的window不可能占据全屏,所以尽管xml文件描述的view不会占满屏幕,而是占满作为对话框的window。所以,如果想调整对话框的大小,就需要设置一下对话框窗口(window)的宽高尺寸:

dialog.show();Window window = dialog.getWindow();WindowManager.LayoutParams params =thisDialog.getWindow().getAttributes();int height = 200;int width = 200;params.height = height;params.width = width;window.setAttributes(params);


此时window的大小是200*200像素,因此视图充斥的空间也就是200*200像素。但是前面说过,xml文件中的按钮背景图没有使用.9图,所以它不会自动缩放,如果按钮使用wrap_content的方式,在200*200的窗口中,会看见两个按钮重叠在一起。

其它一些常用参数设置:

弹出对话框后,如果希望对话框意外的区域颜色变暗,可以做如下设置:

dialog.show();Window window = dialog.getWindow();window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);WindowManager.LayoutParams params =thisDialog.getWindow().getAttributes();params.dimAmount = 0.8f;window.setAttributes(params);


dimAmount是设置对话框意外的区域能见度的,取值范围是0.0f~1.0f,0.0f是全透明,也就是说对话框下面覆盖区域以外的内容是可以看的清清楚楚的,1.0f就是一点也看不见,漆黑一片。但是让dimAmount设置起作用,必须为window加上FLAG_DIM_BEHIND旗标才可以,否则设置无效。

点击对话框以外的区域,对话框不消失:

这个是调用dialog对象本身的一个方法:dialog.setCanceledOnTouchOutside(false);

当将需要的设置都设置完毕后,就可以调用setContentView(view)来添加view到窗口中。然后利用window.findViewById来获取view中定义的子视图,并设置内容或者监听器。

最后,利用dialog来调用setContentView和使用window来调用setContentView效果是一样的,因为Dialog的setContentView方法内部就是使用它持有的window对象的setContentView方法来添加view的:

public void setContentView(int layoutResID){       mWindow.setContentView(layoutResID);}

mWindow是Dialog的一个Window类型的属性,用来引用对话框的窗口对象。

0 0