【问题标题】:Why does minHeight attribute not work in WebView Android?为什么 minHeight 属性在 WebView Android 中不起作用?
【发布时间】:2017-10-10 13:39:30
【问题描述】:

这个问题已经被问到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 25 上尝试过,遇到了这个问题。但我认为这与Android版本无关。
  • pre kit kat 版本中存在一些关于 webview 的问题。所以我问了。

标签: java android android-layout webview view-hierarchy


【解决方案1】:

首先,让我们了解其他视图如何使用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 是最后一个负责测量WebViewwe can clearly see 的组件,它的onMeasure() 不尊重getSuggestedMinimumHeight() 的值。

我不确定这是否是预期行为。尽管如此,我找不到enough seams 以某种方式影响该测量过程。 Here's 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()); } }

如您所见,这不是可以轻松定制/更改的东西。

【讨论】:

    【解决方案2】:

    试试看:

    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>
    

    【讨论】:

    • 你应该考虑到 warnings,lint 给出的。
    • 我们可以通过代码获取。定义 LinearLayout 的高度。
    【解决方案3】:

    我认为 webview 尺寸更适合内容视图端口属性...

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

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

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2017-10-29
        • 2019-02-19
        • 2017-02-16
        • 1970-01-01
        • 1970-01-01
        • 2018-02-06
        • 1970-01-01
        • 1970-01-01
        • 2020-12-29
        相关资源
        最近更新 更多