在android中我们经常会用AlertDialog来显示对话框。通过这个对话框是显示在屏幕中心的。但在某些程序中,要求对话框可以显 示在不同的位置。例如,屏幕的上方或下方。要实现这种效果。就需要获得对话框的Window对象,获得这个Window对象有多种方法。最容易的就是直接 通过AlertDialog类的getWindow方法来获得Window对象。
1 | AlertDialog dialog = new AlertDialog.Builder( this ).setTitle( "title" ) |
2 | .setMessage( "message" ).create(); |
3 | Window window = alertDialog.getWindow(); |
4 | window.setGravity(Gravity.TOP); //window.setGravity(Gravity.BOTTOM); |
5 | alertDialog.show(); |
![android 对话框弹出位置和透明度的设置](https://i-blog.csdnimg.cn/blog_migrate/9a727309fec02f23c835e53397bbae9e.png)
![android 对话框弹出位置和透明度的设置](https://i-blog.csdnimg.cn/blog_migrate/b4e705dd7e20017e37896a10232d6476.png)
透明的对话框
默认显示的对话框是不透明的,但我们可以通过设置对话框的alpha值将其变成透明或半透明效果。我们都知道。颜色由R(红)、 G(绿)、B(蓝)组成。除此之外,还会有一个A(透明度,Alpha)来描述颜色。在颜色的描述中,如果该值为0表示完全透明,如果该值为255,表示 不透明。
通过设置Windows的alpha属性也可以设置对话框的透明度。但alpha的取值范围是从0到1.0。如果该属性值为0,表 示完全透明,如果该值为1.0,表示不透明(也就是正常显示的对话框)。下面的代码通过将alpha的值设为0.3,为了更清晰地显示透明的对话框和非透 明的对话框。在本例中加了一个背景图像,将同时显示了两个对话框(一个是半透明的,另一是不透明的)。
1 | // 显示透明的对话框 |
2 | 4 . AlertDialog alertDialog = new AlertDialog.Builder( this ).setMessage( |
3 | 5 . "透明对话框" ).setPositiveButton( "确定" , null ).create(); |
4 | 6 . Window window = alertDialog.getWindow(); |
5 | 7 . WindowManager.LayoutParams lp = window.getAttributes(); |
6 | 8 . // 设置透明度为0.3 |
7 | 9 . lp.alpha = 0 .6f; |
8 | 10 . window.setAttributes(lp); |
9 | 11 . alertDialog.show(); |
![android 对话框弹出位置和透明度的设置](https://i-blog.csdnimg.cn/blog_migrate/d480112b1b15b1cf33b6f6f7cad0a738.png)
我们在使用某些应用时会发现当弹出对话框或某些模式窗口时,后面的内容会变得模糊或不清楚。实际上,这些效果也很容易在OPhone中实现。为了实现这个功能,我们只需要设置Wndow对象的两个标志即可,代码如下:
- window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
- WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
![android 对话框弹出位置和透明度的设置](https://i-blog.csdnimg.cn/blog_migrate/8c178037a51707f566a84291b82e8923.png)
转自:http://www.open-open.com/lib/view/open1335256482983.html
先贴一下显示效果图,仅作参考:
代码如下:
1、自定义Dialog
public
class
SelectDialog extends AlertDialog{
public SelectDialog(Context context, int theme) {
super(context, theme);
}
public SelectDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slt_cnt_type);
}
}
public SelectDialog(Context context, int theme) {
super(context, theme);
}
public SelectDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slt_cnt_type);
}
}
2、布局文件slt_cnt_type.xml代码
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
< LinearLayout
xmlns:android = " http://schemas.android.com/apk/res/android "
android:orientation = " vertical "
android:padding = " 10dp " android:layout_width = " 115dp " android:layout_height = " wrap_content " android:background = " @color/blue " >
< Button android:layout_height = " wrap_content " android:background = " #00000000 " android:layout_width = " fill_parent " android:text = " 全部联系人 " android:paddingTop = " 5dp " android:paddingBottom = " 5dp " android:paddingLeft = " 10dp " android:gravity = " left|center_vertical " android:id = " @+id/btnSltCntAll " ></ Button >
< Button android:layout_height = " wrap_content " android:background = " #00000000 " style = " @drawable/greenhand_button " android:text = " 咕咚用户 " android:gravity = " left|center_vertical " android:paddingBottom = " 5dp " android:paddingTop = " 5dp " android:paddingLeft = " 10dp " android:paddingRight = " 10dp " android:layout_width = " fill_parent " android:id = " @+id/btnSltGudongUser " ></ Button >
< Button style = " @drawable/greenhand_button " android:background = " #00000000 " android:layout_height = " wrap_content " android:layout_width = " fill_parent " android:text = " 推荐用户 " android:gravity = " left|center_vertical " android:paddingTop = " 5dp " android:paddingBottom = " 5dp " android:paddingLeft = " 10dp " android:id = " @+id/btnSltRecommend " ></ Button >
</ LinearLayout >
< LinearLayout
xmlns:android = " http://schemas.android.com/apk/res/android "
android:orientation = " vertical "
android:padding = " 10dp " android:layout_width = " 115dp " android:layout_height = " wrap_content " android:background = " @color/blue " >
< Button android:layout_height = " wrap_content " android:background = " #00000000 " android:layout_width = " fill_parent " android:text = " 全部联系人 " android:paddingTop = " 5dp " android:paddingBottom = " 5dp " android:paddingLeft = " 10dp " android:gravity = " left|center_vertical " android:id = " @+id/btnSltCntAll " ></ Button >
< Button android:layout_height = " wrap_content " android:background = " #00000000 " style = " @drawable/greenhand_button " android:text = " 咕咚用户 " android:gravity = " left|center_vertical " android:paddingBottom = " 5dp " android:paddingTop = " 5dp " android:paddingLeft = " 10dp " android:paddingRight = " 10dp " android:layout_width = " fill_parent " android:id = " @+id/btnSltGudongUser " ></ Button >
< Button style = " @drawable/greenhand_button " android:background = " #00000000 " android:layout_height = " wrap_content " android:layout_width = " fill_parent " android:text = " 推荐用户 " android:gravity = " left|center_vertical " android:paddingTop = " 5dp " android:paddingBottom = " 5dp " android:paddingLeft = " 10dp " android:id = " @+id/btnSltRecommend " ></ Button >
</ LinearLayout >
3、颜色color.xml代码
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
< resources >
< color name = " transparent " > # 00000000 </ color >
</ resources >
< resources >
< color name = " transparent " > # 00000000 </ color >
</ resources >
4、样式style.xml代码
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
< resources >
< style name = " dialog " parent = " @android:style/Theme.Dialog " >
< item name = " android:windowFrame " > @null </ item ><!-- 边框 -->
< item name = " android:windowIsFloating " > true </ item ><!-- 是否浮现在activity之上 -->
< item name = " android:windowIsTranslucent " > false </ item ><!-- 半透明 -->
< item name = " android:windowNoTitle " > true </ item ><!-- 无标题 -->
< item name = " android:windowBackground " > @color/transparent </ item ><!-- 背景透明 -->
< item name = " android:backgroundDimEnabled " > false </ item ><!-- 模糊 -->
</ style >
</ resources >
< resources >
< style name = " dialog " parent = " @android:style/Theme.Dialog " >
< item name = " android:windowFrame " > @null </ item ><!-- 边框 -->
< item name = " android:windowIsFloating " > true </ item ><!-- 是否浮现在activity之上 -->
< item name = " android:windowIsTranslucent " > false </ item ><!-- 半透明 -->
< item name = " android:windowNoTitle " > true </ item ><!-- 无标题 -->
< item name = " android:windowBackground " > @color/transparent </ item ><!-- 背景透明 -->
< item name = " android:backgroundDimEnabled " > false </ item ><!-- 模糊 -->
</ style >
</ resources >
4、显示Dialog
SelectDialog selectDialog
=
new
SelectDialog(
this
,R.style.dialog);//创建Dialog并设置样式主题
Window win = selectDialog.getWindow();
LayoutParams params = new LayoutParams();
params.x = - 80 ;//设置x坐标
params.y = - 60 ;//设置y坐标
win.setAttributes(params);
selectDialog.setCanceledOnTouchOutside( true );//设置点击Dialog外部任意区域关闭Dialog
selectDialog.show();
Window win = selectDialog.getWindow();
LayoutParams params = new LayoutParams();
params.x = - 80 ;//设置x坐标
params.y = - 60 ;//设置y坐标
win.setAttributes(params);
selectDialog.setCanceledOnTouchOutside( true );//设置点击Dialog外部任意区域关闭Dialog
selectDialog.show();
以上仅作参考,如有问题,请大家及时提出来,一起交流学习!