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

源碼:https://github.com/xiaoyanger0825/BottomDialog