Dialog中webView限制高度

当在Android弹框中使用webView加载内容时,为了防止内容过长撑满屏幕,可以通过自定义webView来限制最大高度。如果内容未超过最大高度,弹框高度自适应;否则,高度被限制在设定值。实现方法包括重写webView的onMeasure方法并设置最大高度,在dialog创建时动态调整webView的高度。
摘要由CSDN通过智能技术生成

        自定义弹框时,弹框中需要webView加载内容,如果webView内容很长,弹框会撑满页面,现在不想让弹框撑满,而是可以自己限制弹框最高高度,内容长度没有达到最高高度时,弹框高度根据内容自适应,内容长度超过最高高度时,弹框高度限制在最高高度,这样就不能直接写死弹框高度,而是要动态设置了。

        第一个想到方法是在webView加载完内容后获取webView高度,然后去设置弹框高度:

        val webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                // 获取webView高度,再设置dialog高度
                webView?.measure(0, 0)
                val height = webView?.measuredHeight ?: 0
                if (height > Resources.getSystem().displayMetrics.heightPixels * 4 / 5) {
                    window?.attributes?.height = Resources.getSystem().displayMetrics.heightPixels * 4 / 5
                }
            }
        }

实验发现无效,webview内容超过4/5屏幕高度,弹框依然会撑满屏幕。

有效方法:

自定义webView,在高度上做设置:

class MaxHeightWebView: WebView {

    private var mMaxHeight = -1

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, privateBrowsing: Boolean) : super(context, attrs, defStyleAttr, privateBrowsing)

    fun setMaxHeight(height: Int) {
        mMaxHeight = height
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (mMaxHeight >- 1 && measuredHeight > mMaxHeight){
            setMeasuredDimension(measuredWidth, mMaxHeight)
        }
    }

}

dialog的布局文件中使用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/shape_white_top_corner_r10"
    android:paddingLeft="15dp"
    android:paddingTop="25dp"
    android:paddingRight="15dp"
    android:paddingBottom="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="false"
    android:overScrollMode="never"
    android:orientation="vertical">

    <TextView
        android:id="@+id/titleTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:layout_gravity="center_horizontal"
        android:text="重要提示"
        android:textSize="18sp"
        android:textColor="#DE000000"
        android:textStyle="bold"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <com.view.MaxHeightWebView
            android:id="@+id/contentWV"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>

    <TextView
        android:id="@+id/confirmTV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dpg"
        android:background="@drawable/shape_corner_5_solid_primary"
        android:padding="10dp"
        android:gravity="center"
        android:text="我知道了"
        android:textSize="18sp"
        android:textColor="@android:color/white"/>

</LinearLayout>

dialog在OnCreate时候,设置下webView的最大高度

webView?.setMaxHeight(PixValue.m.heightPixels * 3 / 5)

运行代码,达到我们想要的效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值