WebView in CoordinatorLayout with CollapsingToolbarLayout

匿名 (未验证) 提交于 2019-12-03 02:45:02

问题:

I'd like to add a WebView in a CoordinatorLayout, having CollapsingToolbarLayout in AppBarLayout. The problem is that WebView shrinks in height and doesn't fill the space below the toolbar, making it not usable. I tried also to use WebView as child of NestedScrollView: initially it doesn't work (it shrinks my WebView in height), but scrolling my shrinked WebView makes it fill viewport.

Here is the layout I used:

<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout     android:id="@+id/main_content"     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:fitsSystemWindows="true">      <android.support.design.widget.AppBarLayout         android:id="@+id/appbar"         android:layout_width="match_parent"         android:layout_height="256dp"         android:fitsSystemWindows="true"         android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">          <android.support.design.widget.CollapsingToolbarLayout             android:id="@+id/collapsing_toolbar"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:fitsSystemWindows="true"             app:contentScrim="?attr/colorPrimary"             app:expandedTitleMarginEnd="64dp"             app:expandedTitleMarginStart="48dp"             app:layout_scrollFlags="scroll|exitUntilCollapsed">              <ImageView                 android:id="@+id/backdrop"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:fitsSystemWindows="true"                 android:scaleType="centerCrop"                 app:layout_collapseMode="parallax" />              <android.support.v7.widget.Toolbar                 android:id="@+id/toolbar"                 android:layout_width="match_parent"                 android:layout_height="?attr/actionBarSize"                 app:layout_collapseMode="pin"                 app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />          </android.support.design.widget.CollapsingToolbarLayout>      </android.support.design.widget.AppBarLayout>      <android.support.v4.widget.NestedScrollView         android:layout_width="match_parent"         android:layout_height="match_parent"         app:layout_behavior="@string/appbar_scrolling_view_behavior">          <WebView             android:id="@+id/article"             android:layout_width="match_parent"             android:layout_height="match_parent" />     </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout> 

回答1:

You can use android:fillViewport="true" for NestedScrollView and layout_height="wrap_content" for WebView



回答2:

The bug is now fixed in the latest (22.2.1) design support library.



回答3:

I think set app:layout_scrollFlags="scroll|enterAlways" will be helpful



回答4:

Try to remove the attribute android:fitsSystemWindows="true" from all views.



回答5:

This library seems to have workarounded this issue by extending the WebView class to add the property behaviour: https://github.com/takahirom/webview-in-coordinatorlayout



回答6:

    DisplayMetrics metrics = new DisplayMetrics();     getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);      int statusBarHeight = getStatusBarHeight(this);      TypedArray actionbarSizeTypedArray = this.obtainStyledAttributes(new int[]{android.R.attr.actionBarSize});     int actionBarHeight = (int) actionbarSizeTypedArray.getDimension(0, 0);      NestedScrollView nestedScrollView = (NestedScrollView) findViewById(R.id.nsv_scroll);     CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(             CoordinatorLayout.LayoutParams.MATCH_PARENT,             metrics.heightPixels - actionBarHeight - statusBarHeight);      nestedScrollView.setLayoutParams(params); 

The method getStatusBarHeight() :

public int getStatusBarHeight(Context context){     Class<?> c = null;     Object obj = null;     Field field = null;     int x = 0;     int statusBarHeight = 0;     try {         c = Class.forName("com.android.internal.R$dimen");         obj = c.newInstance();         field = c.getField("status_bar_height");         x = Integer.parseInt(field.get(obj).toString());         statusBarHeight = context.getResources().getDimensionPixelSize(x);     } catch (Exception e1) {         e1.printStackTrace();     }     return statusBarHeight; } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!