说明:
ScrollView是一个顶层布局,可以实现大于手机屏幕的布局,从而实现布局中的内容超过一屏显示不下时可自动为页面提供上下滑动浏览功能。
常用参数:
- 滚动条不显示:android:scrollbars=”none”
- 滚动条恒显示:android:fadeScrollbars=”false”
- 设置水平滚动条的drawable(如颜色):android:scrollbarThumbHorizontal
- 设置垂直滚动条的drawable(如颜色):android:scrollbarThumbVertical
- 设置水平滚动条背景(轨迹)的色drawable(如颜色):android:scrollbarTrackHorizontal
- 设定滚动条宽度:android:scrollbarSize
示例代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/item1"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/item2"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/item3"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/item4"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/item5"
/>
</LinearLayout>
</ScrollView>
注意事项:
1,ScrollView控件是顶层控件尽量放在layout的最外层使用。
2,ScrollView控件只支持垂直滚动,如果要横向滚动请使用:HorizontalScrollView
3,ScrollView中只能包含一个控件,当超过一个时app运行时会崩溃,切记!
4,如果ScrollView并非最外层的控件,请在ScrollView内部再使用一个LinearLayout把这N个组件归在一起。务必!
常规使用方法:
通常是在〈ScrollView〉标签中定义一个<LinearLayout> 容器标签并且在<LinearLayout>标签中将android:orientation属性值设置为 vertical,然后在<LinearLayout〉标签中放置多个控件,如果 <LinearLayout>标签中的控件所占用的总高度超出屏幕的高度,就会 在屏幕的右侧出现一个滚动条。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/139905.html