dialog高度限制

Android的dialog布局如果过高的话可能会产生显示不完全的问题,而设置成全屏显然不符合要求

为了保证能自由高度,又不全屏,不妨碍点击空白dismiss它,则需要一个全屏的透明view,内部再嵌套咱们自己的自定义高度的view,将根部局设置为全屏,再重写点击事件即可。

缺点是需要一个根部局和一个有id的内部布局:

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#00000000">
    <!-- 根部局设置为透明 -->
    <!-- 接下来的内容布局需要有一个id叫dialog_content,可以配合代码做出对应修改 -->
    <LinearLayout
        android:id="@+id/dialog_content"
        android:layout_centerInParent="true"
        android:layout_width="780dp"
        android:layout_height="1200dp"
        android:background="@drawable/round_white"
        android:orientation="vertical"
        android:gravity="center_horizontal">

       <!-- 你的内容 -->

    </LinearLayout>
</RelativeLayout>
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //获取根部局
        val _rootView: View =
            LayoutInflater.from(context).inflate(layoutId, null)
        setContentView(_rootView)
        //设定根部局全屏
        window!!.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN)

        window!!.decorView.setPadding(0, 0, 0, 0)
        val lp: WindowManager.LayoutParams = window!!.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        //设定为全屏,解除高度限制
        lp.height = WindowManager.LayoutParams.MATCH_PARENT
        window!!.attributes = lp

        //点击事件传递到的时候拦截消耗
        _rootView.setOnTouchListener { v, event ->
            dismiss()
            true
        }

        //点击事件传递到的时候消耗掉,别传到rootview去
        //实现点击正式布局不会消失,点击外部时会消失
        var content_View = _rootView.findViewById<View>(R.id.dialog_content)
        content_View.isFocusable = true
        content_View.isClickable = true
        content_View.setOnTouchListener { v, event -> true }

    }