国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現底部對話框BottomDialog彈出實例代碼

2019-10-23 18:34:18
字體:
來源:轉載
供稿:網友

最近項目上需要實現一個底部對話框,要實現這樣的功能其實很簡單,先看代碼:

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();}

對話框的樣式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>

在對話框中的按鈕需要MD風格的波紋效果的話,對話框的style的parent需要設定parent="@style/Base.V7.Theme.AppCompat.Light.Dialog",否則沒有效果。同時將對話框所在window的標題去掉。android:windowBackground屬性一定要設置成透明,否則自定義形狀的對話框背景就是默認的白色了。如果不設置為透明,比如我們通常要設置的圓角對話框就沒有效果。

對話框顯示時從底部進入,關閉時從底部滑出。動畫樣式:

<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>

實現底部對話框的原理就是修改對話框的內容布局contentView的參數,使它的寬度剛好等于屏幕的寬度,并且設置對話框所在Window的gravity屬性為bottom。

需要注意的是,上面代碼中需要在調用contentView.getLayoutParams()需要在setContentView方法后,否則獲取到的LayoutParams為null,當然也可以自己new一個LayoutParams設置給contentView。

dialog底部彈出,android,底部對話框,bottomdialog

如果是要實現底部圓角對話框,原理也相似,只需要給contentView添加一個圓角的背景shape,并減小contentView的寬度給左右兩邊留一定的距離,同時給底部設置邊距。

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();}

dialog底部彈出,android,底部對話框,bottomdialog

 源碼:BottomDialog.rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 积石山| 军事| 枞阳县| 南涧| 临沧市| 盐津县| 沙坪坝区| 墨玉县| 建阳市| 满洲里市| 阳东县| 肃南| 洪江市| 思茅市| 安仁县| 衡阳县| 桂阳县| 枞阳县| 南和县| 台北县| 津市市| 德化县| 沙湾县| 南阳市| 剑川县| 绍兴县| 富宁县| 博野县| 宁城县| 兴化市| 松溪县| 乐清市| 衡南县| 锡林浩特市| 福清市| 彭水| 宁国市| 平武县| 崇文区| 三亚市| 福泉市|