android - 使用 CoordinatorLayout、Swipe2Refresh 和 WebView 时布局顶部的工具栏

标签 android webview android-design-library swiperefreshlayout

我在 web View 顶部显示工具栏时遇到问题,我正在尝试找出解决方法。非常感谢任何和所有反馈。

网页 View .xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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"
    tools:context="com.example.ui.activities.WebActivity">

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_web" />

</android.support.design.widget.CoordinatorLayout>

content_web.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipe_refresh">

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

</android.support.v4.widget.SwipeRefreshLayout>

这里有一些关于我如何设置 webview 的代码。

public class WebActivity extends BaseActivity {
    @Bind(R.id.webview)
    WebView webView;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.swipe_refresh)
    public SwipeRefreshLayout swipeRefreshLayout;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        setSupportActionBar(toolbar);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.reload();
            }
        });

        if (savedInstanceState == null) {
            webViewUrl = AUTH_URL;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    webView = initWebView();
                    webView.loadUrl(webViewUrl);
                }
            });

        } else {
            webViewUrl = savedInstanceState.getString(KEY_URL,AUTH_URL);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    webView.loadUrl(webViewUrl);
                }
            });
        }
    }
}

我们将不胜感激任何帮助。

最佳答案

CoordinatorLayout 中的布局需要定义一个layout_behavior。将您的内容更改为:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    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:id="@+id/swipe_refresh"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    >

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

</android.support.v4.widget.SwipeRefreshLayout>

关于android - 使用 CoordinatorLayout、Swipe2Refresh 和 WebView 时布局顶部的工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33662972/

相关文章:

c# - 如何从代码中清除 Windows 8.1 WebView 的缓存?

android.support.design.button.MaterialButton 抛出 InflateException

android - Fragment 内带有工具栏的 SearchView

java - 从 JavaScript 向上调用 JavaFX

javascript - Chrome 应用 <webview> 标签无法更改背景颜色

android - ActionBar 与工具栏或 ActionBar 和工具栏

Android Studio 签名的 APK 未安装

android - 在 Mac 上为 Android 应用程序设置 Trigger.io 时出错

android - FirebaseFirestore 已经启动,无法再更改其设置

android - 更新有序的 BroadcastReceiver android 之间的 Intent extras