Android 自定义底部弹出对话框

Android About 3,446 words

底部对话框

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

对话框样式

对话框中的按钮需要MD风格的波纹效果的话,对话框的styleparent需要设定parent="@style/Theme.AppCompat.Light.Dialog",否则没有效果。同时将对话框所在window的标题去掉。android:windowBackground属性一定要设置成透明,否则自定义形状的对话框背景就是默认的白色了。如果不设置为透明,比如我们通常要设置的圆角对话框就没有效果。

<style name="BottomDialog" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

动画样式

对话框显示时从底部进入,关闭时从底部滑出

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

对话框中控件

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:drawableLeft="@drawable/ic_circle"
        android:drawablePadding="16dp"
        android:gravity="center_vertical"
        android:padding="16dp"
        android:text="设置朋友圈权限"
        android:textColor="#666666"
        android:textSize="14sp"/>

实现底部对话框的原理就是修改对话框的内容布局 contentView 的参数,使它的宽度刚好等于屏幕的宽度,并且设置对话框所在 Window 的 gravity 属性为 bottom 。 需要注意的是,上面代码中需要在调用 contentView.getLayoutParams() 需要在 setContentView 方法后,否则获取到的 LayoutParams 为 null ,当然也可以自己 new 一个 LayoutParams 设置给 contentView。

bottom_dialog.gif

底部圆角对话框

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

bd366aad6070.gif

Views: 6,006 · Posted: 2019-04-11

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh