Nestedscrollview / Scrollview内部的Webview缺少内容

时间:2018-06-25 05:41:56

标签: android android-webview android-nestedscrollview

我有一个放置在nestedscrollview中的webview。我面临的问题是webview无法加载整个页面。而是加载页面的一部分,然后将底部空间保留为空白(白色)。

我也尝试过使用scrollview。

我得到的输出:

Output i am getting

“我的网络视图代码”:

private ImageButton backButton;
    private WebView webView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article);

        backButton = findViewById(R.id.backButton);
        webView=(WebView)findViewById(R.id.webView);
        webView.setWebViewClient(new MyBrowser());
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//            descriptionView.setText(Html.fromHtml(data, Html.FROM_HTML_MODE_COMPACT));
//        } else {
//            descriptionView.setText(Html.fromHtml(data));
//        }
        webView.getSettings().setJavaScriptEnabled(true);

        webView.loadUrl("file:///android_asset/html/test.html");

        backButton.setOnClickListener(this);
    }

    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activities.ArticleActivity">


    <RelativeLayout
        android:id="@+id/topBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary">

        <ImageButton
            android:id="@+id/backButton"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:background="@null"
            card_view:srcCompat="@drawable/outline_arrow_back_ios_24" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_centerInParent="true"
            android:layout_margin="20dp"
            android:scaleType="centerInside"
            android:src="@drawable/x01" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/topBar">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.CardView
                android:id="@+id/card_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                card_view:cardCornerRadius="2dp">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/iconView"
                        android:layout_width="match_parent"
                        android:layout_height="120dp"
                        android:layout_centerVertical="true"
                        android:scaleType="centerCrop"
                        android:src="@drawable/article" />

                    <com.virtual_antivirus.virtualantivirus.Utlis.BanglaTextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="10dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginTop="10dp"
                        android:text="ভার্চুয়াল ভাইরাসের সর্বনাশা শিকার"
                        android:textColor="@android:color/black"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <LinearLayout
                        android:layout_width="100dp"
                        android:layout_height="5dp"
                        android:layout_marginLeft="10dp"
                        android:background="@color/colorAccent"></LinearLayout>

                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:minHeight="300dp">

                        <WebView
                            android:id="@+id/webView"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent" />
                    </FrameLayout>


                </LinearLayout>


            </android.support.v7.widget.CardView>
        </ScrollView>

    </LinearLayout>


    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:layout_below="@+id/topBar"
        android:background="@drawable/shadow" />

</RelativeLayout>

6 个答案:

答案 0 :(得分:2)

  

您可以将NestedScrollView放置在协调器布局中,并且必须禁用滚动行为,并且如果使用应用程序栏,还必须放置布局行为。

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:nestedScrollingEnabled="false"
app:layout_behavior="appbar_scrolling_view_behavior">
.....   

答案 1 :(得分:0)

使用

android:fillViewport =“ true”

内部滚动视图属性

答案 2 :(得分:0)

您可以使用NestedScrollView并将此行放入Webview

 android:nestedScrollingEnabled="false"

然后检查您的xml类

答案 3 :(得分:0)

像这样更新您的XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:id="@+id/topBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary">

    <ImageButton
        android:id="@+id/backButton"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:background="@null" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:scaleType="centerInside" />
</RelativeLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/topBar">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        card_view:cardCornerRadius="2dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/iconView"
                android:layout_width="match_parent"
                android:layout_height="120dp"
                android:layout_gravity="center_horizontal"
                android:scaleType="centerCrop" />

            <com.virtual_antivirus.virtualantivirus.Utlis.BanglaTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="10dp"
                android:text="ভার্চুয়াল ভাইরাসের সর্বনাশা শিকার"
                android:textColor="@android:color/black"
                android:textSize="18sp"
                android:textStyle="bold" />

            <WebView
                android:id="@+id/webView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:minHeight="300dp" />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</ScrollView>

<View
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:layout_below="@+id/topBar" />

</RelativeLayout>

如果不能解决您的问题,请尝试此操作,然后选中此post

答案 4 :(得分:0)

我猜是因为您的HTML。我尝试了其他HTML页面。一切正常。

尝试使用以下html文件

 <html >
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<body style="word-wrap: break-word;" >
AasdfasfasfsaffafafafafaaafafafffAasdfasfa<br/>sfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafff
AasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaf<br/>fafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafff
AasdfasfasfsaffafafafafaaafafafffAasdfasfasfsa<br/>ffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafff
AasdfasfasfsaffafafafafaaafafafffAasdfasfasf<br/>saffafafafafaaafafafffAasdv<br/>fasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafff
Aasdfasfasfsaffafafafafaaaf<br/>afafffAasdfasfasfsaffafafafafaaafafafffAasdfasf<br/>asfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafff
Aasdfasfasfsaffafafafafaaafafa<br/>fffAasdfasfasfsaffafafafafa<br/>aafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafffAasdfasfasfsaffafafafafaaafafafff
</body>
</html>

答案 5 :(得分:0)

这就是我这样做的方式,它也可以与最新的API一起正常工作,并且实际上是将来进行滚动(隐藏Toolbar或..)行为等的最佳方法。

您需要做的就是将您的内容放在NestedScrollView内并添加:

android:nestedScrollingEnabled="true"

WebView

在您的onCreate()方法中:

WebView webView = findViewById(R.id.webView);
webView.loadUrl("file:///android_asset/htmlPages/test.html");

布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginEnd="10dp"
                app:cardUseCompatPadding="true">

                <WebView
                    android:id="@+id/webView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:nestedScrollingEnabled="true" />

            </androidx.cardview.widget.CardView>
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

输出:

enter image description here

我什至使用了AndroidX依赖关系,您可以将其替换为自己的支持库标签依赖关系。