WebView in CoordinatorLayout with CollapsingToolbarLayout

≡放荡痞女 提交于 2019-12-02 18:10:24

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

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

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

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

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

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