这个问题已经被问了here,但是没有解决方案。
我有一个WebView
。我想使用WebView
属性为minHeight
设置最小高度,但是不起作用。相同的属性适用于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
属性。有人知道这个问题的解决方案吗?最佳答案
首先,让我们了解其他 View 如何使用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
。WebView#onMeasure()
delegates the job to WebViewChromium#onMeasure()
WebViewChromium#onMeasure()
delegates the job to AwContents#onMeasure()
AwContents#onMeasure()
delegates the job to AwLayoutSizer#onMeasure
AwLayoutSizer
是负责测量WebView
和we 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()); } }
如您所见,这不是可以轻松自定义/更改的内容。