Android實(shí)現(xiàn)底部對(duì)話框BottomDialog

最近項(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ì)話框的styleparent需要設(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ì)話框所在Windowgravity屬性為bottom

需要注意的是,上面代碼中需要在調(diào)用contentView.getLayoutParams()需要在setContentView方法后,否則獲取到的LayoutParamsnull,當(dāng)然也可以自己new一個(gè)LayoutParams設(shè)置給contentView。

底部對(duì)話框效果

如果是要實(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();
}

底部圓角對(duì)話框效果

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容