关于Android 滚屏控件:ScrollView 使用的一些细节和注意事项

54 篇文章 2 订阅

说明:

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>标签中的控件所占用的总高度超出屏幕的高度,就会 在屏幕的右侧出现一个滚动条。 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值