Android實(shí)現(xiàn)底部對(duì)話框BottomDialog彈出實(shí)例代碼
最近項(xiàng)目上需要實(shí)現(xiàn)一個(gè)底部對(duì)話框,要實(shí)現(xiàn)這樣的功能其實(shí)很簡(jiǎn)單,先看代碼:
private void show1() { Dialog bottomDialog = new Dialog(this, R.style.BottomDialog); View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_content_normal, null); bottomDialog.setContentView(contentView); ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams(); layoutParams.width = getResources().getDisplayMetrics().widthPixels; contentView.setLayoutParams(layoutParams); bottomDialog.getWindow().setGravity(Gravity.BOTTOM); bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation); bottomDialog.show(); }
對(duì)話框的樣式style:
<style name="BottomDialog" parent="@style/Base.V7.Theme.AppCompat.Light.Dialog"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> </style>
在對(duì)話框中的按鈕需要MD風(fēng)格的波紋效果的話,對(duì)話框的style的parent需要設(shè)定parent="@style/Base.V7.Theme.AppCompat.Light.Dialog",否則沒有效果。同時(shí)將對(duì)話框所在window的標(biāo)題去掉。android:windowBackground屬性一定要設(shè)置成透明,否則自定義形狀的對(duì)話框背景就是默認(rèn)的白色了。如果不設(shè)置為透明,比如我們通常要設(shè)置的圓角對(duì)話框就沒有效果。
對(duì)話框顯示時(shí)從底部進(jìn)入,關(guān)閉時(shí)從底部滑出。動(dòng)畫樣式:
<style name="BottomDialog.Animation" parent="Animation.AppCompat.Dialog"> <item name="android:windowEnterAnimation">@anim/translate_dialog_in</item> <item name="android:windowExitAnimation">@anim/translate_dialog_out</item> </style>
tranlate_dialog_in.xml:
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromXDelta="0" android:fromYDelta="100%" android:toXDelta="0" android:toYDelta="0"> </translate>
tranlate_dialog_out.xml:
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="100%"> </translate>
實(shí)現(xiàn)底部對(duì)話框的原理就是修改對(duì)話框的內(nèi)容布局contentView的參數(shù),使它的寬度剛好等于屏幕的寬度,并且設(shè)置對(duì)話框所在Window的gravity屬性為bottom。
需要注意的是,上面代碼中需要在調(diào)用contentView.getLayoutParams()需要在setContentView方法后,否則獲取到的LayoutParams為null,當(dāng)然也可以自己new一個(gè)LayoutParams設(shè)置給contentView。
如果是要實(shí)現(xiàn)底部圓角對(duì)話框,原理也相似,只需要給contentView添加一個(gè)圓角的背景shape,并減小contentView的寬度給左右兩邊留一定的距離,同時(shí)給底部設(shè)置邊距。
private void show2() { Dialog bottomDialog = new Dialog(this, R.style.BottomDialog); View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_content_circle, null); bottomDialog.setContentView(contentView); ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams(); params.width = getResources().getDisplayMetrics().widthPixels - DensityUtil.dp2px(this, 16f); params.bottomMargin = DensityUtil.dp2px(this, 8f); contentView.setLayoutParams(params); bottomDialog.getWindow().setGravity(Gravity.BOTTOM); bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation); bottomDialog.show(); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- Android自定義對(duì)話框Dialog的簡(jiǎn)單實(shí)現(xiàn)
- 詳解Android 全局彈出對(duì)話框SYSTEM_ALERT_WINDOW權(quán)限
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
- Android 之BottomsheetDialogFragment仿抖音評(píng)論底部彈出對(duì)話框效果(實(shí)例代碼)
- Android實(shí)現(xiàn)退出界面彈出提示對(duì)話框
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- Android仿QQ消息提示實(shí)現(xiàn)彈出式對(duì)話框
- Android對(duì)話框使用方法詳解
相關(guān)文章
Android開發(fā)手冊(cè)Chip監(jiān)聽及ChipGroup監(jiān)聽
這篇文章主要為大家介紹了Android開發(fā)手冊(cè)Chip監(jiān)聽及ChipGroup監(jiān)聽,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06簡(jiǎn)單實(shí)現(xiàn)Android學(xué)生管理系統(tǒng)(附源碼)
這篇文章主要介紹了如何簡(jiǎn)單實(shí)現(xiàn)Android學(xué)生管理系統(tǒng),特別適合計(jì)算機(jī)專業(yè)的即將畢業(yè)的同學(xué)學(xué)習(xí)借鑒制作學(xué)生管理系統(tǒng),感興趣的小伙伴們可以參考一下2015-12-12Android實(shí)現(xiàn)Unity3D下RTMP推送的示例
像Unity3D下的RTMP或RTSP播放器一樣,好多開發(fā)者苦于在Unity環(huán)境下,如何高效率低延遲的把數(shù)據(jù)采集并編碼實(shí)時(shí)推送到流媒體服務(wù)器,實(shí)現(xiàn)Unity場(chǎng)景下的低延遲推拉流方案。本文介紹幾種RTMP推送的方案2021-06-06kotlin中數(shù)據(jù)類重寫setter getter的正確方法
這篇文章主要給大家介紹了關(guān)于kotlin中數(shù)據(jù)類重寫setter getter的正確方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用kotlin具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06Android 10 啟動(dòng)分析之init語(yǔ)法詳解
這篇文章主要為大家介紹了Android 10 啟動(dòng)分析之init語(yǔ)法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10非常簡(jiǎn)單的Android打開和保存對(duì)話框功能
這篇文章主要介紹了非常簡(jiǎn)單的Android打開和保存對(duì)話框功能,感興趣的小伙伴們可以參考一下2016-07-07