使用 CoordinatorLayout、Swipe2Refresh 和 WebView 时布局顶部的工具栏
Toolbar on top of layout when using CoordinatorLayout, Swipe2Refresh and WebView
我在 web 视图顶部显示工具栏时遇到问题,我正在尝试找出解决方法。非常感谢任何和所有反馈。
webview.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>
我在 web 视图顶部显示工具栏时遇到问题,我正在尝试找出解决方法。非常感谢任何和所有反馈。
webview.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>