<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#123456" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#abcdef"
android:padding="100dp"
android:text="张欣爱我一生一世" />
</ScrollView>
</RelativeLayout>
添加了这句话android:padding="100dp"才可以滑动,去掉不能滑动,如果把
android:layout_height=”wrap_content”改成
android:layout_height=”500dp”还是不能滑动。
android:padding=”100dp”`就是改变了它的内容(内边距属于内容)。
所以我们得出一个结论,当ScrollView只有一个控件的时候,它的高度不起作用,能不能滚动取决于他内容的高度。如
`ScrollView只能有一个直接的儿子,多了会报错
我们在看一个典型的错误
在父容器ScrollView中写的是 android:layout_height=”wrap_content”
(父容器高度由子容器决定),而在LinearLayout中写的是android:layout_height=”match_parent”(子容器的高度由父容器决定),相互矛盾,会报错。
总结下
(1)对于ScrollView只有两种显示方式:充满父容器, 包裹类型
(2)当只有一个控件,而且控件的内容高度没有填充我们的ScrollView
那么设置控件的高度是没有效果的
(3)ScrollView能不能滚动,是由控件的内容高度决定的,不是由高度决定的。
(4)ScrollView只能有一个直接的子控件。
(5)ScrollView高度一般不要用wrap_content 直接子控件不要要用match_parent