FrameLayout中Toolbar随滚动隐藏/显示功能失效问题求助
嘿,我来帮你搞定这个Toolbar滚动不隐藏的问题~
问题出在哪?
你现在的布局结构里有个关键矛盾:
- 根布局的
FrameLayout已经加了app:layout_behavior="@string/appbar_scrolling_view_behavior"——这个属性是给CoordinatorLayout用的,用来让它感知滚动容器的滚动,进而联动AppBarLayout(也就是你的Toolbar所在的布局)。 - 但你注入进去的
NestedScrollView又重复加了同一个Behavior,而且FrameLayout本身不是滚动容器,CoordinatorLayout根本没法“看穿”FrameLayout,感知到里面NestedScrollView的滚动动作,自然就不会触发Toolbar的隐藏/显示了。
怎么修复?
只需要两步,非常简单:
- 删掉
SecondActivity布局里NestedScrollView的app:layout_behavior="@string/appbar_scrolling_view_behavior"属性 - 确保根布局的
FrameLayout保留这个Behavior(你已经做了,不用改)
修改后的SecondActivity布局代码
<android.support.v4.widget.NestedScrollView xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:fillViewport="true" android:gravity="center" android:paddingLeft="20dp" android:paddingRight="20dp"> <TextView android:id="@+id/textone" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/content" android:textSize="25sp" /> <!-- 顺手提一句:文字大小建议用sp,比dp更适配系统字体缩放 --> </LinearLayout> </android.support.v4.widget.NestedScrollView>
额外小贴士
- 如果你以后换成RecyclerView这类滚动容器,同样要记住只有最外层的FrameLayout需要设置
appbar_scrolling_view_behavior,内部的滚动容器别加这个属性。 - 确保你的滚动容器是CoordinatorLayout支持的类型(比如NestedScrollView、RecyclerView),普通的LinearLayout/RelativeLayout没法触发滚动联动哦。
内容的提问来源于stack exchange,提问作者Naveen Kumar
相关产品推荐
相关产品推荐

