简体   繁体   English

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

[英]Why does minHeight attribute not work in WebView Android?

This question has already been asked here , but it has no solution. 这个问题已在这里提出,但它没有解决方案。

I have a WebView . 我有一个WebView I want to set minimum height to the WebView using minHeight attribute, but it doesn't work. 我想使用minHeight属性为WebView设置最小高度,但它不起作用。 The same attribute works for Button. 相同的属性适用于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 !!"/>

Clearly from the below image, the WebView is not supporting the minHeight attribute. 显然,从下图中,WebView不支持minHeight属性。 Does anybody knows a solution for this problem? 有谁知道这个问题的解决方案?

在此输入图像描述

First, let's understand how other view's use android:minHeight attribute. 首先,让我们了解其他视图如何使用android:minHeight属性。 Let's take Spinner for example. 我们以Spinner为例。 In AbsSpinner#onMeasure() code we see following chunk of code: 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);
...

So, getSuggestedMinimumHeight() should be regarded when computing preferred height. 因此,在计算首选高度时应考虑getSuggestedMinimumHeight()

Now, let's see how WebView is being measured. 现在,让我们看看如何测量WebView

AwLayoutSizer is the last component that is responsible for measuring WebView and we can clearly see , that its onMeasure() does not respect getSuggestedMinimumHeight() value. AwLayoutSizer是负责测量WebView的最后一个组件, 我们可以清楚地看到它的onMeasure()不尊重getSuggestedMinimumHeight()值。

I'm not sure whether this is an intended behavior or no. 我不确定这是预期的行为还是不行。 Nevertheless, I cannot find enough seams to somehow affect that measurement process. 然而,我找不到足够的接缝以某种方式影响测量过程。 Here's the chuck of code in WebView class, where the object that eventually would return WebViewChromium (the first step in abovementioned order) is initialized. 这是 WebView类中的代码,其中最终将返回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());
        }
    }

As you can see, this is not something that can be easily customized/changed. 如您所见,这不是可以轻松定制/更改的内容。

Try it : 试试吧 :

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>

在此输入图像描述

I think webview dimensions work more according to content view port properties... 我认为webview维度根据内容视图端口属性更有效...

Check https://developer.android.com/guide/webapps/targeting.html#Viewport 检查https://developer.android.com/guide/webapps/targeting.html#Viewport

The viewport is the area in which your web page is drawn. 视口是绘制网页的区域。 Although the viewport's total visible area matches the size of the screen when zoomed all the way out, the viewport has its own pixel dimensions that it makes available to a web page. 虽然视口的总可见区域在完全缩放时与屏幕大小相匹配,但视口具有自己的像素尺寸,可供网页使用。 For example, although a device screen might have physical a width of 480 pixels, the viewport can have a width of 800 pixels. 例如,虽然设备屏幕的物理宽度可能为480像素,但视口的宽度可以为800像素。 This allows a web page designed at 800 pixels wide to be completely visible on the screen when the viewport scale is 1.0. 当视口比例为1.0时,这允许设计为800像素宽的网页在屏幕上完全可见。 Most web browsers on Android (including Chrome) set the viewport to a large size by default (known as "wide viewport mode" at about 980px wide). Android上的大多数Web浏览器(包括Chrome)默认情况下将视口设置为大尺寸(称为“宽视口模式”,宽度约为980px)。 Many browsers also zoom out as far as possible, by default, to show the full viewport width (known as "overview mode"). 默认情况下,许多浏览器也会尽可能缩小以显示完整的视口宽度(称为“概览模式”)。

use constraint layout .that will help to resolve all these types of errors and very easy to use. 使用约束布局。这将有助于解决所有这些类型的错误,并且非常易于使用。

if your android studio version is below 2.3.1 then add this dependency. 如果您的android studio版本低于2.3.1,则添加此依赖项。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM