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

其實(shí)Dialog, DialogFragment, Activity 能看到的界面,都是基于Window顯示的;
也就是修改樣式, 都是在修改window的樣式;
所以,本質(zhì)上方法都是一樣的,唯一不同的就是獲取window對(duì)象的方法不一樣;


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


有了window對(duì)象, 就可以開始本文了:

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之后, 調(diào)用;并且 setBackgroundDrawable 必須設(shè)置
//這里的-1,-2可以設(shè)置為任意高度;

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這個(gè)view
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//注意此處
    window.setLayout(-1, -2);//這2行,和上面的一樣,注意順序就行;
    return view;
}

3:附贈(zèng)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();
}

Winodw的其實(shí)屬性修改:

mWindow = getDialog().getWindow();
//無標(biāo)題
mWindow.requestFeature(Window.FEATURE_NO_TITLE);//必須放在setContextView之前調(diào)用
rootView = (ViewGroup) inflater.inflate(R.layout.rsen_base_dialog_fragment_layout,
(ViewGroup) mWindow.findViewById(android.R.id.content));
//透明狀態(tài)欄
mWindow.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//退出,進(jìn)入動(dòng)畫
mWindow.setWindowAnimations(getAnimStyles());
//清理背景變暗 
mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//點(diǎn)擊window外的區(qū)域 是否消失
getDialog().setCanceledOnTouchOutside(canCanceledOnOutside());
//是否可以取消,會(huì)影響上面那條屬性
setCancelable(canCancelable());
//window外可以點(diǎn)擊,不攔截窗口外的事件
mWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
//設(shè)置背景顏色,只有設(shè)置了這個(gè)屬性,寬度才能全屏MATCH_PARENT
mWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
WindowManager.LayoutParams mWindowAttributes = mWindow.getAttributes();
mWindowAttributes.width = getWindowWidth();//這個(gè)屬性需要配合透明背景顏色,才會(huì)真正的 MATCH_PARENT
mWindowAttributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
//gravity
mWindowAttributes.gravity = getGravity();
mWindow.setAttributes(mWindowAttributes);
//沒啦,更多屬性可以在API文檔里面查看.
mLayoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN// 覆蓋狀態(tài)欄
                | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL//窗口外可以點(diǎn)擊
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE//不監(jiān)聽按鍵事件
//                    | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
//                    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN
                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS//突破窗口限制
//                    | WindowManager.LayoutParams.FLAG_FULLSCREEN
        ;

至此: 文章就結(jié)束了,如有疑問: QQ群:274306954 歡迎您的加入.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容