布局中搭配android.support.v7.widget.Toolbar时ScrollView无法正常工作
问题分析与解决方案
这问题其实很好排查——你的根LinearLayout默认是**水平排列(horizontal)**的,再加上你给Toolbar设置了android:layout_width="fill_parent",直接占满了整个水平屏幕宽度,ScrollView被完全挤到屏幕外面去了,自然看不到里面的按钮和内容啦!
修复方案
只需要两个小改动就能解决:
- 给根LinearLayout添加垂直排列属性,让Toolbar和ScrollView上下摆放
- 调整ScrollView的高度为
match_parent,让它占满Toolbar下方的剩余屏幕空间
修改后的完整布局代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 新增垂直排列属性 --> <include android:id="@+id/toolbar" layout="@layout/myToolBarRed" android:layout_margin="2dp" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 改为match_parent占满剩余高度 --> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <!-- Content here --> <Button android:text="Button" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button1" /> </LinearLayout> </ScrollView> </LinearLayout>
小提示
fill_parent是旧版本API的写法,现在推荐统一使用match_parent,两者在新版SDK中功能完全一致,代码风格更统一。
内容的提问来源于stack exchange,提问作者Mehrdad Ghasemi
相关产品推荐
相关产品推荐

