Why doesn't minHeight attribute work in Android WebView?

This question has already been asked here , but it has no solution.

I have one WebView. I want to set the minimum height in WebViewusing the attribute minHeight, but this will not work. The same attribute works for 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 minHeight is set to 150 dp and it works !!"/>

Obviously, from the image below, WebView does not support the attribute minHeight. Does anyone know a solution to this problem?

enter image description here

+6
source share
4 answers

-, android: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.

  • WebView#onMeasure() WebViewChromium#onMeasure()
  • WebViewChromium#onMeasure() AwContents#onMeasure()
  • AwContents#onMeasure() AwLayoutSizer#onMeasure

AwLayoutSizer - , WebView , onMeasure() getSuggestedMinimumHeight() getSuggestedMinimumHeight().

, . , , - . WebView WebView, 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());
        }
    }

, , /.

+8

:

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 minHeight is set to 150 dp and it works !!"/>
    </RelativeLayout>

enter image description here

0

, - ...

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

- , -. , , -. , 480 , 800 . - 800 , 1,0. - Android ( Chrome) ( " " 980 ). , , , ( " " ).

0
source

use a restriction pattern. which will help resolve all these types of errors and is very easy to use.

if your version for Android version is lower than 2.3.1, add this dependency.

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

Source: https://habr.com/ru/post/1017176/


All Articles