Dialog/DialogFragment寬度高度修改/全屏,自定義樣式

其實Dialog, DialogFragment, Activity 能看到的界面,都是基於Window顯示的; 
也就是修改樣式, 都是在修改window的樣式; 
因此,本質上方法都是同樣的,惟一不一樣的就是獲取window對象的方法不同;android

Dialog 經過, getWindow() 獲取; 
Activity 也是經過, getWindow() 獲取; 
DialogFragment 則是getDialog().getWindow()獲取;ide

有了window對象, 就能夠開始本文了:動畫

1:修改Dialog中window寬度和高度.net

//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方法中blog

@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 
版權聲明:本文爲博主原創文章,轉載請附上博文連接!文檔

相關文章
相關標籤/搜索