为什么 minHeight 属性在 WebView Android 中不起作用?

Why does minHeight attribute not work in WebView Android?

这个问题已经有人问过here,但是没有解决方案。

我有一个WebView。我想使用 minHeight 属性将最小高度设置为 WebView,但它不起作用。相同的属性适用于 Button。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.anshul.webview.WebActivity">

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="400dp"></WebView>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:minHeight="150dp"
    android:text="This is a Button. It's minHeight is set to 150 dp and it works !!"/>

从下图中可以清楚地看出,WebView 不支持 minHeight 属性。有人知道这个问题的解决方案吗?

首先,让我们了解其他视图如何使用android:minHeight属性。我们以 Spinner 为例。在 AbsSpinner#onMeasure() 代码中,我们看到以下代码块:

...
preferredHeight = Math.max(preferredHeight, getSuggestedMinimumHeight());
preferredWidth = Math.max(preferredWidth, getSuggestedMinimumWidth());

heightSize = resolveSizeAndState(preferredHeight, heightMeasureSpec, 0);
widthSize = resolveSizeAndState(preferredWidth, widthMeasureSpec, 30);

setMeasuredDimension(widthSize, heightSize);
...

因此,计算首选身高时应考虑getSuggestedMinimumHeight()

现在,让我们看看 WebView 是如何衡量的。

AwLayoutSizer is the last component that is responsible for measuring WebView and we can clearly see,其 onMeasure() 不符合 getSuggestedMinimumHeight() 值。

我不确定这是否是预期的行为。然而,我无法在 WebView class 中找到 enough seams to somehow affect that measurement process. Here's 代码块,其中最终将 return WebViewChromium 的对象(上述顺序的第一步)已初始化。


    private void ensureProviderCreated() {
        checkThread();
        if (mProvider == null) {
            // As this can get called during the base class constructor chain, pass the minimum
            // number of dependencies here; the rest are deferred to init().
            mProvider = getFactory().createWebView(this, new PrivateAccess());
        }
    }

如你所见,这不是一件容易的事情customized/changed。

试一试:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView = (WebView)findViewById(R.id.webView);
        webView.loadDataWithBaseURL(null, "<html><body bgcolor=\"#E6E6FA\"> hehehe </body></html>", "text/html", "utf-8", null);

    }



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/blue"
    android:layout_height="match_parent">
    <LinearLayout
        android:minHeight="300dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        >
        <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:minHeight="150dp"
        android:text="This is a Button. It's minHeight is set to 150 dp and it works !!"/>
    </RelativeLayout>

我认为 webview 维度根据内容视图端口属性更有效...

勾选https://developer.android.com/guide/webapps/targeting.html#Viewport

视口是绘制网页的区域。虽然视口的总可见区域在完全缩小时与屏幕大小相匹配,但视口有自己的像素尺寸,可用于网页。例如,虽然设备屏幕的物理宽度可能为 480 像素,但视口的宽度可以为 800 像素。这允许设计为 800 像素宽的网页在视口比例为 1.0 时在屏幕上完全可见。 Android(包括 Chrome)上的大多数网络浏览器默认将视口设置为较大尺寸(称为 "wide viewport mode",宽度约为 980 像素)。许多浏览器也会尽可能地缩小,默认情况下,以显示完整的视口宽度(称为 "overview mode")。

使用约束布局。这将有助于解决所有这些类型的错误并且非常易于使用。

如果您的 android 工作室版本低于 2.3.1,则添加此依赖项。

compile 'com.android.support.constraint:constraint-layout:1.0.0-beta1'