这个问题已经有人问过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
属性。有人知道这个问题的解决方案吗?
最佳答案
首先,让我们了解其他 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 toWebViewChromium#onMeasure()
WebViewChromium#onMeasure()
delegates the job toAwContents#onMeasure()
AwContents#onMeasure()
delegates the job toAwLayoutSizer#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());
}
}
如您所见,这不是可以轻松定制/更改的东西。
关于java - 为什么 minHeight 属性在 WebView Android 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43917326/