首先自定义一个类,j继承自dialog类。
设置透明度,主要设置的是dialog自身的透明度
- WindowManager.LayoutParams lp=getWindow().getAttributes();
- lp.alpha=1.0f;
- getWindow().setAttributes(lp);
WindowManager.LayoutParams lp=dialog.getWindow().getAttributes();
lp.alpha=1.0f;
dialog.getWindow().setAttributes(lp);
alpha在0.0f到1.0f之间。1.0完全不透明,0.0f完全透明,自身不可见。
设置幕布,也就是本dialog的背景层。
- WindowManager.LayoutParams lp=dialog.getWindow().getAttributes();
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
- lp.dimAmount=1.0f;
- getWindow().setAttributes(lp);
dialog.setContentView(R.layout.dialog);
WindowManager.LayoutParams lp=dialog.getWindow().getAttributes();
lp.dimAmount=1.0f;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dimAmount在0.0f和1.0f之间,0.0f完全不暗,即背景是可见的 ,1.0f时候,背景全部变黑暗。
如果要达到背景全部变暗的效果,需要设置 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
,否则,背景无效果。
此方法可以用来设置浮动层。呵呵!