解決Android中自定義DialogFragment解決寬度和高度問題
關(guān)于詳解Android應(yīng)用中DialogFragment的基本用法,大家可以參考下。
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至少需要實(shí)現(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>
總結(jié)
以上所述是小編給大家介紹的解決Android中自定義DialogFragment解決寬度和高度問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android自定義Dialog的2種常見方法
- Android自定義Dialog框樣式
- Android自定義Dialog原理實(shí)例解析
- Android 自定義加載動畫Dialog彈窗效果的示例代碼
- Android自定義底部彈出框ButtomDialog
- android自定義Dialog彈框和背景陰影顯示效果
- Android自定義Dialog實(shí)現(xiàn)通用圓角對話框
- Android自定義dialog 自下往上彈出的實(shí)例代碼
- Android 自定義Dialog去除title導(dǎo)航欄的解決方法
- Android自定義Dialog實(shí)現(xiàn)加載對話框效果
- Android編程自定義AlertDialog樣式的方法詳解
- Android?自定義?Dialog?實(shí)現(xiàn)列表?單選、多選、搜索功能
相關(guān)文章
通過Html網(wǎng)頁調(diào)用本地安卓(android)app程序代碼
如何使用html網(wǎng)頁和本地app進(jìn)行傳遞數(shù)據(jù)呢?經(jīng)過研究,發(fā)現(xiàn)還是有方法的,總結(jié)了一下,大致有一下幾種方式2013-11-11
android使用ExpandableListView控件實(shí)現(xiàn)小說目錄效果的例子
這篇文章主要介紹了android使用ExpandableListView控件實(shí)現(xiàn)小說目錄效果的例子,還可以實(shí)現(xiàn)二級列表展示效果,需要的朋友可以參考下2014-07-07
解析Android開發(fā)優(yōu)化之:對界面UI的優(yōu)化詳解(一)
在Android應(yīng)用開發(fā)過程中,屏幕上控件的布局代碼和程序的邏輯代碼通常是分開的。界面的布局代碼是放在一個獨(dú)立的xml文件中的,這個文件里面是樹型組織的,控制著頁面的布局2013-05-05
Android中Service實(shí)時向Activity傳遞數(shù)據(jù)實(shí)例分析
這篇文章主要介紹了Android中Service實(shí)時向Activity傳遞數(shù)據(jù)的方法,實(shí)例分析了Service組件基于線程操作實(shí)現(xiàn)數(shù)值實(shí)時傳遞的相關(guān)技巧,需要的朋友可以參考下2015-09-09
另外兩種Android沉浸式狀態(tài)欄實(shí)現(xiàn)思路
這篇文章主要為大家介紹了另外兩種Android沉浸式狀態(tài)欄實(shí)現(xiàn)思路,android5.0及以后版本都支持給狀態(tài)欄著色,而目前android主流版本還是4.4,想要深入了解的朋友可以參考一下2016-01-01
clipse項(xiàng)目遷移到android studio的方法(圖文最新版)
這篇文章主要介紹了clipse項(xiàng)目遷移到android studio(圖文最新版),需要的朋友可以參考下2015-10-10
Android ActivityManagerService啟動流程詳解
這篇文章主要介紹了Android ActivityManagerService啟動流程,AMS,即ActivityManagerService,是安卓java framework的一個服務(wù),運(yùn)行在system_server進(jìn)程。此服務(wù)十分重要,因?yàn)樗芾碇沧康乃拇蠼M件,是安卓APP開發(fā)者最常接觸到的一個服務(wù)2023-02-02
超簡單Android集成華為HMS Scankit 掃碼SDK實(shí)現(xiàn)掃一掃二維碼

