其实Dialog, DialogFragment, Activity 能看到的界面,都是基于Window显示的;
也就是修改样式, 都是在修改window的样式;
所以,本质上方法都是一样的,唯一不同的就是获取window对象的方法不一样;
Dialog 通过, getWindow() 获取;
Activity 也是通过, getWindow() 获取;
DialogFragment 则是getDialog().getWindow()获取;
有了window对象, 就可以开始本文了:
1:修改Dialog中window宽度和高度
//public static final int FILL_PARENT = -1;
//public static final int MATCH_PARENT = -1;
//public static final int WRAP_CONTENT = -2;
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//注意此处
dialog.setContentView(textView, new ViewGroup.LayoutParams(-2, -2));
dialog.getWindow().setLayout(-1, -2);//setLayout必须 在 setContentView之后, 调用;并且 setBackgroundDrawable 必须设置
//这里的-1,-2可以设置为任意高度;
1
2
3
4
5
6
7
2:修改DialogFragment中window宽度和高度
在DialogFragment的onCreateView()方法中
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final Window window = getDialog().getWindow();
View view = inflater.inflate(R.layout.dialog_fragment_layout, ((ViewGroup) window.findViewById(android.R.id.content)), false);//需要用android.R.id.content这个view
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//注意此处
window.setLayout(-1, -2);//这2行,和上面的一样,注意顺序就行;
return view;
}
1
2
3
4
5
6
7
3:附赠Activity中修改window的宽度和高度
在Activity的onAttachedToWindow方法中
@Override
public void onAttachedToWindow() {
Window window = getWindow();
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.width = 460;
attributes.height = 700;
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//注意此处
getWindow().setAttributes(attributes);
super.onAttachedToWindow();
}
1
2
3
4
5
6
7
8
9
10
Winodw的其实属性修改:
mWindow = getDialog().getWindow();
//无标题
mWindow.requestFeature(Window.FEATURE_NO_TITLE);//必须放在setContextView之前调用
rootView = (ViewGroup) inflater.inflate(R.layout.rsen_base_dialog_fragment_layout,
(ViewGroup) mWindow.findViewById(android.R.id.content));
//透明状态栏
mWindow.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//退出,进入动画
mWindow.setWindowAnimations(getAnimStyles());
//清理背景变暗
mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//点击window外的区域 是否消失
getDialog().setCanceledOnTouchOutside(canCanceledOnOutside());
//是否可以取消,会影响上面那条属性
setCancelable(canCancelable());
//window外可以点击,不拦截窗口外的事件
mWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
//设置背景颜色,只有设置了这个属性,宽度才能全屏MATCH_PARENT
mWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
WindowManager.LayoutParams mWindowAttributes = mWindow.getAttributes();
mWindowAttributes.width = getWindowWidth();//这个属性需要配合透明背景颜色,才会真正的 MATCH_PARENT
mWindowAttributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
//gravity
mWindowAttributes.gravity = getGravity();
mWindow.setAttributes(mWindowAttributes);
//没啦,更多属性可以在API文档里面查看.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
mLayoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN// 覆盖状态栏
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL//窗口外可以点击
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE//不监听按键事件
// | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
// | WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS//突破窗口限制
// | WindowManager.LayoutParams.FLAG_FULLSCREEN
;
---------------------
作者:angcyo
来源:CSDN
原文:https://blog.csdn.net/angcyo/article/details/50613084
版权声明:本文为博主原创文章,转载请附上博文链接!
来源:oschina
链接:https://my.oschina.net/u/1771562/blog/2252601