ScrollView 多向上滚动一段pading
时间: 2024-05-15 22:12:47 浏览: 132
Android ScrollView取消惯性滚动的方法
要实现ScrollView多向上滚动一段padding,你可以在ScrollView中添加一个内部容器(例如LinearLayout),然后在该容器中添加一个空的View,高度设置为padding值。这样,在ScrollView滚动时,它会滚动到内部容器的顶部,而不是直接滚动到顶部。具体实现代码如下:
```xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- 添加一个空的View,高度为padding值 -->
<View
android:layout_width="match_parent"
android:layout_height="20dp"/>
<!-- 其他子View -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod nisl sit amet quam tincidunt blandit. Nunc semper ex non nisl convallis, vel imperdiet est lobortis. Donec at sem eget nulla luctus laoreet vel sit amet mauris. Maecenas metus magna, tincidunt a lorem ut, tincidunt tempus nunc. Nam feugiat enim ac sapien pulvinar, eu varius mauris commodo. Sed non risus quis ligula maximus bibendum. Nam eu nisi leo. "/>
<!-- 其他子View -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sed euismod nisl sit amet quam tincidunt blandit. Nunc semper ex non nisl convallis, vel imperdiet est lobortis. Donec at sem eget nulla luctus laoreet vel sit amet mauris. Maecenas metus magna, tincidunt a lorem ut, tincidunt tempus nunc. Nam feugiat enim ac sapien pulvinar, eu varius mauris commodo. Sed non risus quis ligula maximus bibendum. Nam eu nisi leo. "/>
<!-- 其他子View -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nunc semper ex non nisl convallis, vel imperdiet est lobortis. Donec at sem eget nulla luctus laoreet vel sit amet mauris. Maecenas metus magna, tincidunt a lorem ut, tincidunt tempus nunc. Nam feugiat enim ac sapien pulvinar, eu varius mauris commodo. Sed non risus quis ligula maximus bibendum. Nam eu nisi leo. "/>
<!-- 其他子View -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nunc semper ex non nisl convallis, vel imperdiet est lobortis. Donec at sem eget nulla luctus laoreet vel sit amet mauris. Maecenas metus magna, tincidunt a lorem ut, tincidunt tempus nunc. Nam feugiat enim ac sapien pulvinar, eu varius mauris commodo. Sed non risus quis ligula maximus bibendum. Nam eu nisi leo. "/>
</LinearLayout>
</ScrollView>
```
上面的例子中,我们在LinearLayout中添加了一个高度为20dp的空View,这就是我们的padding值。当ScrollView滚动时,它会滚动到空View的顶部,从而实现了多向上滚动一段padding的效果。
阅读全文