最近比较闲,手头事情也不是很多,所以就想学习一下CoordinatorLayout这玩意,这玩意确实不错,至少动画上面不用我们操心的太多。
但是在将CoordingnatorLayout和Webview联合使用的时候,我发现,它并没有按照我原先预想的那样表现☞当Webview内容可以滑动时,向上滑动Webview,Toolbar应该是要进行隐藏的,然而并没有,然后自己琢磨了一番,上网搜刮了一些资料,这里给出一个办法,一方面自己记录一下,方便以后查看,另一方便,也可以给和我一样的初学者一个解决方案。
一开始,我的布局是这样的↓
<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:orientation="vertical"
tools:context="com.kenny.hurry.WebActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/id_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="10dp"
app:layout_scrollFlags="scroll|enterAlways"
app:navigationIcon="@drawable/selector_back"
app:titleTextColor="@color/primary_material_light" />
</android.support.design.widget.AppBarLayout>
<WebView
android:id="@+id/wb_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>
反复查看了代码,反复检查使用CoordinatorLayout滑动的几个条件:
- CoordinatorLayout作为布局的父布局容器。
- 给需要滑动的组件设置 app:layout_scrollFlags=”scroll|enterAlways” 属性。
- 给滑动的组件设置app:layout_behavior属性。
看来看去,发现都满足了呀,也给Webview添加了属性
app:layout_behavior="@string/appbar_scrolling_view_behavior"
发现还是不行。
最后上网翻阅了好多资料,才有了点头绪,事实上,滑动的并不是WebView本身,而是其加载网页的内容,所以给它设置该属性是没有用的,于是乎,我给它套上了NestedScrollView,结果真的可以了!!!纠结了我几小时。
修改后的布局是这样的↓
<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:orientation="vertical"
tools:context="com.kenny.hurry.WebActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/id_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="10dp"
app:layout_scrollFlags="scroll|enterAlways"
app:navigationIcon="@drawable/selector_back"
app:titleTextColor="@color/primary_material_light" />
</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/wb_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
/(ㄒoㄒ)/~~
希望我惨痛的经历能给大家一些帮助。