设置Dialog宽高自适应方案

废话不多说,先上图,然后上代码,徐某的文章就是这么的直入主题
首先是正常显示,宽高为wrap_parent


image.png

设置宽度为match_parent 高度为wrap_parent


image.png

设置高度为match_parent 宽度为wrap_parent


image.png

设置宽高为match_parent


image.png

第一步:首先继承Dialog(其实不继承也可以,但是主要在里面封装一些方法,容易复用与维护)

public class DialogX extends Dialog {
    /**
     * @param context     上下文
     * @param layout      视图
     * @param themeResId       风格
     */
    public DialogX(Activity context, View layout, int themeResId) {
        super(context, themeResId);
        setContentView(layout);
    }

    /**
     * 初始化Dialog
     * @param width 设置宽度
     * @param height 设置高度
     * @param gravity 设置显示位置
     * @param isCancelable 是否禁用back键
     * @param animation 设置动画资源文件
     * @param isAnimation 设置是否开启动画
     */
    public void initDialog(int width, int height, int gravity, boolean isCancelable,int animation, boolean isAnimation) {
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        //设置宽
        switch (width) {
            case WindowManager.LayoutParams.MATCH_PARENT:
                layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
                break;
            case WindowManager.LayoutParams.WRAP_CONTENT:
                layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
                break;
            default:
                layoutParams.width = (int) dp2px(getContext(), width);
                break;
        }
        //设置高
        switch (height) {
            case WindowManager.LayoutParams.MATCH_PARENT:
                layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
                break;
            case WindowManager.LayoutParams.WRAP_CONTENT:
                layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
                break;
            default:
                layoutParams.height = (int) dp2px(getContext(), width);
                break;
        }
        //设置显示位置
        layoutParams.gravity = gravity;
        //设置是否屏蔽返回键与点击空白区域不关闭Dialog
        setCancelable(isCancelable);
        //设置是否开启动画
        if (isAnimation) {
            //如果动画资源为空,则设置成默认动画
            if (animation != 0) {
                layoutParams.windowAnimations = animation;
            } else {
                layoutParams.windowAnimations = R.style.Animation_Design_BottomSheetDialog;
            }
        }
        //设置属性
        getWindow().setAttributes(layoutParams);
    }

    public float dp2px(Context context, int dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (dpValue * scale + 0.5f);
    }
}
 <!-- Dialog样式-->
    <style name="DialogTheme" parent="@android:style/Theme.Dialog">
        <!-- 边框 -->
        <item name="android:windowFrame">@null</item>
        <!-- 是否浮现在activity之上 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 半透明 -->
        <item name="android:windowIsTranslucent">true</item>
        <!-- 无标题 -->
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@android:color/transparent</item>
        <!-- 背景透明 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!-- 模糊 -->
        <item name="android:backgroundDimEnabled">true</item>
        <!-- 遮罩层 -->
        <item name="android:backgroundDimAmount">0.5</item>
    </style>

    <!--Dialog 从底部弹出,底部收回动画-->
    <style name="BottomInAndOutStyle">
        <item name="android:windowEnterAnimation">@anim/in_bottom</item>
        <item name="android:windowExitAnimation">@anim/out_bottom</item>
    </style>

    <!--Dialog 从顶部弹出,顶部回收动画-->
    <style name="TopInAndOutStyle">
        <item name="android:windowEnterAnimation">@anim/anim_enter_top</item>
        <item name="android:windowExitAnimation">@anim/anim_exit_bottom</item>
    </style>

这里是风格文件中所引用的动画资源文件(如果不设置动画的话,可以跳过,在需要注释DialogX中//设置是否开启动画的区域代码)


image.png

anim_enter_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromYDelta="-100%p"
        android:toYDelta="0"/>
    <alpha
        android:duration="500"
        android:fromAlpha="0.5"
        android:toAlpha="1.0"/>
</set>

anim_exit_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:toAlpha="0.5"/>
    <translate
        android:duration="1000"
        android:fromYDelta="0"
        android:toYDelta="-100%p"/>
</set>

in_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="300"
        android:fromYDelta="100%p"
        android:toYDelta="0"/>
</set>

out_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="300"
        android:fromYDelta="100%p"
        android:toYDelta="0"/>
</set>

布局文件:dialog_test.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_shape"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:text="你确定要退出登录吗?"
        android:textColor="@android:color/black" />
    <View
        android:id="@+id/line"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_below="@id/tv_title"
        android:layout_marginTop="30dp"
        android:background="#CCC" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/line"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_determine"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="确定"
            android:textColor="#FF0000" />
        <View
            android:layout_width="0.5dp"
            android:layout_height="match_parent"
            android:background="#CCC" />
        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="取消" />
    </LinearLayout>
</RelativeLayout>

工具类:DialogHelper(编写一些静态方法,方便调用)
DialogHelper.java

public class DialogHelper {
    /**
     * 显示代码
     *
     * @param context
     * @return
     */
    public static void showDialog(Activity context) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_test, null);
        
      /*注意dialogX.initDialog()方法,第一个参数为宽,第二个参数为高,如果设置为WindowManager.LayoutParams.MATCH_PARENT则等同于match_parent,设置成WindowManager.LayoutParams.WRAP_CONTENT,则等同于wrap_parent,设置成值则为xdp,这里进行了px转dp操作,所以只需要填值即可
*/
        DialogX dialogX = new DialogX(context,view,R.style.DialogTheme);
        dialogX.show();
        dialogX.initDialog(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                Gravity.CENTER,true,0,true
        );
    }
}

使用(调用工具类方法,即可)

DialogHelper.showDialog(MainActivity.this);

有问题可以在下方评论,我看到会第一时间回复你,这就是我,话不多说的徐某人,徐某人不谈原理,只助你CV

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容