Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有折叠工具栏布局的协调布局中的WebView_Android_Android Design Library - Fatal编程技术网

Android 带有折叠工具栏布局的协调布局中的WebView

Android 带有折叠工具栏布局的协调布局中的WebView,android,android-design-library,Android,Android Design Library,我想在CoordinatorLayout中添加一个WebView,在AppBarLayout中具有折叠工具栏布局。 问题是WebView的高度会缩小,无法填充工具栏下方的空间,因此无法使用。 我还尝试将WebView用作NestedScrollView的子视图:最初它不起作用(它会在高度上缩小我的WebView),但滚动缩小的WebView会使它填充视口 以下是我使用的布局: <?xml version="1.0" encoding="utf-8"?> <android.sup

我想在CoordinatorLayout中添加一个WebView,在AppBarLayout中具有折叠工具栏布局。 问题是WebView的高度会缩小,无法填充工具栏下方的空间,因此无法使用。 我还尝试将WebView用作NestedScrollView的子视图:最初它不起作用(它会在高度上缩小我的WebView),但滚动缩小的WebView会使它填充视口

以下是我使用的布局:

<?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>

尝试从所有视图中删除属性
android:fitsystemwindows=“true”

    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);
方法
getStatusBarHeight()

public int getStatusBarHeight(上下文){
c类=空;
objectobj=null;
字段=空;
int x=0;
int statusBarHeight=0;
试一试{
c=Class.forName(“com.android.internal.R$dimen”);
obj=c.newInstance();
字段=c.getField(“状态栏高度”);
x=Integer.parseInt(field.get(obj.toString());
statusBarHeight=context.getResources().getDimensionPixelSize(x);
}捕获(异常e1){
e1.printStackTrace();
}
返回高度;
}

最新(22.2.1)的设计支持库修复了该漏洞。

我认为set
app:layout\u scrollFlags=“scroll | enterally”
会很有帮助

这个库似乎已经解决了这个问题,它扩展了WebView类来添加属性行为:

您可以使用android:fillViewport=“true”对于NestedScrollView和
layout\u height=“wrap\u content”
对于WebView

这对我很有用

<?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"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/backgroundColor"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|enterAlways|snap">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom">

                <androidx.appcompat.widget.AppCompatImageButton
                    android:id="@+id/back_bt"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:background="@color/transparent"
                    android:src="@drawable/ic_arrow_back_black_24dp" />
            </FrameLayout>

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

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

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

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

            <WebView
                android:id="@+id/web_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>


它不起作用:安装系统窗口不会拉伸视图,并且需要安装系统窗口才能通过AppBarLayout获取状态栏scrim。好的。我在实现上还有一个问题,但是我已经用webview构建了一个testapp。您可以在本文中查看xml布局文件:。我的apptheme是这样的:我刚刚看到了你的问题:如何用java代码实例化你的webview;wvTest.setWebViewClient(新WebViewClient(){@Override public boolean shouldOverrideUrlLoading(WebView视图,字符串url){return super.shouldOverrideUrlLoading(视图,url);}});wvTest.loadUrl(“);和我实例化我的一样…我不知道为什么它没有填满我的所有布局!请你解释一下为什么这会有助于解决提问者的问题?嗯…“度量”在这里没有定义@Jiashu
DisplayMetrics=new DisplayMetrics();getActivity().getWindowManager().getDefaultDisplay().getMetrics(度量);
自最新版本(24.2.0)以来,该漏洞又出现了
<?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"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/backgroundColor"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|enterAlways|snap">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom">

                <androidx.appcompat.widget.AppCompatImageButton
                    android:id="@+id/back_bt"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:background="@color/transparent"
                    android:src="@drawable/ic_arrow_back_black_24dp" />
            </FrameLayout>

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

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

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

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

            <WebView
                android:id="@+id/web_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>