今天就跟大家聊聊有關(guān)Android中怎么通過自定義DialogFragment解決寬度和高度,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)建站聯(lián)系熱線:18980820575,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù),創(chuàng)新互聯(lián)建站網(wǎng)頁制作領(lǐng)域十多年,包括水泥攪拌車等多個領(lǐng)域擁有豐富的網(wǎng)站制作經(jīng)驗,選擇創(chuàng)新互聯(lián)建站,為網(wǎng)站錦上添花!
1、 概述
DialogFragment在android 3.0時被引入。是一種特殊的Fragment,用于在Activity的內(nèi)容之上展示一個模態(tài)的對話框。典型的用于:展示警告框,輸入框,確認(rèn)框等等。
在DialogFragment產(chǎn)生之前,我們創(chuàng)建對話框:一般采用AlertDialog和Dialog。注:官方不推薦直接使用Dialog創(chuàng)建對話框。
2、 好處與用法
使用DialogFragment來管理對話框,當(dāng)旋轉(zhuǎn)屏幕和按下后退鍵時可以更好的管理其聲明周期,它和Fragment有著基本一致的聲明周期。且DialogFragment也允許開發(fā)者把Dialog作為內(nèi)嵌的組件進(jìn)行重用,類似Fragment(可以在大屏幕和小屏幕顯示出不同的效果)。上面會通過例子展示這些好處~
使用DialogFragment至少需要實現(xiàn)onCreateView或者onCreateDIalog方法。onCreateView即使用定義的xml布局文件展示Dialog。onCreateDialog即利用AlertDialog或者Dialog創(chuàng)建出Dialog。
下面通過示例代碼給大家介紹下Android中自定義DialogFragment解決寬度和高度問題
Android中自定義DialogFragment解決寬度和高度問題但是我們很多時候想把DialogFragment的高度固定,那么我們需要設(shè)置DialogFragment的高度,在Fragment的onResume()聲明周期方法中設(shè)置window的寬高即可。
@Override public void onResume() { super.onResume(); getDialog().getWindow().setLayout(DeviceUtil.getDeviceWidth(), ResUtils.dp2px(295)); }
設(shè)置DialogFrament 從底部彈出,并且彈出動畫為向上滑出,消失動畫為向下滑出
WindowManager.LayoutParams params = getDialog().getWindow() .getAttributes(); params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL; params.windowAnimations = R.style.bottomSheet_animation; getDialog().getWindow().setAttributes(params);
完整的代碼如下:
@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { WindowManager.LayoutParams params = getDialog().getWindow() .getAttributes(); params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL; params.windowAnimations = R.style.bottomSheet_animation; getDialog().getWindow().setAttributes(params); getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().setCanceledOnTouchOutside(true); getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); mContentView = inflater.inflate(R.layout.fragment_create_quick, container, false); return mContentView; } @Override public void onResume() { super.onResume(); getDialog().getWindow().setLayout(DeviceUtil.getDeviceWidth(), HlyUtils.dp2px(380)); } <style name="bottomSheet_animation" parent="@android:style/Animation"> <item name="android:windowEnterAnimation">@anim/slide_in_bottom</item> <item name="android:windowExitAnimation">@anim/slide_out_bottom</item> </style> <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:duration="300" android:fromYDelta="100.0%p" android:toYDelta="0.0" /> </set> <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:duration="300" android:fromYDelta="0%p" android:toYDelta="100%p" /> </set>
看完上述內(nèi)容,你們對Android中怎么通過自定義DialogFragment解決寬度和高度有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
文章名稱:Android中怎么通過自定義DialogFragment解決寬度和高度
網(wǎng)址分享:http://bm7419.com/article34/goicse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、云服務(wù)器、App設(shè)計、網(wǎng)頁設(shè)計公司、、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)