Android开发之对话框高级应用

Android开发之对话框高级应用

        创建并显示一个对话框非常easy。可是假设想进行一些更高级点的操作,就须要一些技巧了。以下将和大家分享一下对话框使用的一些高级技巧。


1.改变对话框的显示位置:


        大多说情况下。对话框是在屏幕的中间位置显示的(依据系统版本号的不同可能有所差异)。

实际上,也能够依据须要在屏幕的左、中、右、上、下。甚至任何位置来显示对话框。

        要想获取对话框的显示的位置,须要获得对话框的Window对象,并通过Window对象的一些方法来控制对话框的显示位置。

比如:

AlertDialog alertDialog=new AlertDialog.Builder(this).setMessage("在屏幕顶端显示对话框").setPositiveButton("确定", null).create();
Window window=alertDialog.getWindow();
/*调用setGravity方法使对话框在屏幕顶端显示。

*/ window.setGravity(Gravity.TOP); alertDialog.show();


提示setGravity方法的參数也支持的例如以下几个值:

Gravity.LEFT(左側)。Gravity.RIGHT(右側),Gravity.BOTTOM(底部)Gravity.LEFT|Gravity.TOP(左上方)等。        

执行效果图:

改变对话框的显示位置


2.在任何位置显示对话框:


        上面已经获取了对话框的Window对象,我们能够通过该对象的getAttributes()方法来获取WindowManager.LayoutParams对象。并通过WindowManager.LayoutParams对象的WindowManager.LayoutParams.x与WindowManager.LayoutParams.y属性来设置对话框在屏幕中的位置。

AlertDialog alertDialog=new AlertDialog.Builder(this).setMessage("在任何位置显示对话框").setPositiveButton("确定", null).create();
Window window=alertDialog.getWindow();                    
WindowManager.LayoutParams wl=window.getAttributes();
//x,y为对话框相对于屏幕的中心位置的偏移量
//设置水平偏移量
wl.x=-50;
//设置垂直偏移量
wl.y=100;
window.setAttributes(wl);
alertDialog.show();

提示通过WindowManager.LayoutParams.xWindowManager.LayoutParams.y设置的是对话框相对于屏幕中间位置的偏移量,并非屏幕的绝对位置。

执行效果图:

在任何位置显示对话框的显


3在对话框的内容文本和button中插入图片度:


       我曾在Android开发之TextView高级应用中介绍过在TextView中插入图像,这样的技术在对话框中也相同适用。

在对话框中插入图片的代码例如以下

/**
 * 在对话框的内容文本和button中插入图片度
 * */
protected void insertImg() {
         // TODO Auto-generated method stub
         AlertDialog alertDialog=new AlertDialog.Builder(this)
         .setMessage(getHtml()).setPositiveButton(getHtml(), null).create();          
         alertDialog.show();
}
//返回一个Spanned对象
private Spanned getHtml() {
         // TODO Auto-generated method stub
         Spanned spanned=Html.fromHtml("哈哈。<img src=''/>你好.", new ImageGetter() {
                   @Override
                   public Drawable getDrawable(String source) {
                            // TODO Auto-generated method stub
                            Drawable drawable=getResources().getDrawable(R.drawable.ic_launcher);
                            //设置图片的缩放大小
                            drawable.setBounds(0,0,30,30);
                            return drawable;
                   }
         }, null);
         return spanned;
}

执行效果图:

在对话框的内容文本和button中插入图片度


4.改变对话框的透明度


        通过WindowManager.LayoutParams.alpha能够设置对话框的透明度。Alpha的取值范围在0.0f1.0f之间(f表示float类型的数字)。0.0f表示全然透明(这时对话框就看不见了)。1.0f表示全然不透明1.0f也是alpha的默认值。以下的代码显示了一个Alpha值为0.6f的个对话框。

/**
 * 改变对话框的透明度
 * */
protected void setAlpha() {
         // TODO Auto-generated method stub
         AlertDialog alertDialog=new AlertDialog.Builder(this)
         .setMessage("这是一个透明度为60%的对话框!").setPositiveButton("确定", null).create();
         Window window=alertDialog.getWindow();
         window.setGravity(Gravity.TOP);
         WindowManager.LayoutParams wl=window.getAttributes();
         //alpha的范围是0.0f-1.0f
         wl.alpha=0.6f;
         window.setAttributes(wl);
         alertDialog.show();
}

执行效果图:

改变对话框的透明度

原文地址:https://www.cnblogs.com/clnchanpin/p/6728322.html