ScrollView是一个容器控件,在这个容器中的内容可以滚动。
例子XML
<LinearLayout 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"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="ScrollView例子" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="14dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
<!-- 此处可以添加很多布局内容,在当前页面中滚动 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="ScrollView例子" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="14dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
下面列出ScrollView的一些属性:
- android:background 设置背景色/背景图片。可以通过以下两种方法设置背景为透明:”和”。注意 TextView默认是透明的,不用写此属性,但是Buttom/ImageButton/ImageView想透明的话就得写这个属性了。
- android:clickable 是否响应点击事件。
- android:contentDescription 设置View的备注说明,作为一种辅助功能提供,为一些没有文字描述的View提供说明,如ImageButton。这里在界面上不会有效果,自己在程序中控制,可临时放一点字符串数据。
- android:drawingCacheQuality 设置绘图时半透明质量。有以下值可设置:auto(默认,由框架决定)/high(高质量,使用较高的颜色深度,消耗更多的内存)/low(低质量,使用较低的颜色深度,但是用更少的内存)。
- android:duplicateParentState 如果设置此属性,将直接从父容器中获取绘图状态(光标,按下等)。 见下面代码部分,注意根据目前测试情况仅仅是获取绘图状态,而没有获取事件,也就是你点一下LinearLayout时Button有被点击的效果,但是不执行点击事件。
- android:fadingEdge 设置拉滚动条时 ,边框渐变的放向。none(边框颜色不变),horizontal(水平方向颜色变淡),vertical(垂直方向颜色变淡)。参照fadingEdgeLength的效果图
- android:fadingEdgeLength 设置 边框渐变的长度。
- android:fitsSystemWindows 设置布局调整时是否考虑系统窗口(如状态栏)
- android:focusable 设置是否获得焦点。若有requestFocus()被调用时,后者优先处理。注意在表单中想设置某一个如EditText获取焦点,光设置这个是不行的,需要将这个EditText前面的focusable都设置为false才行。在Touch模式下获取焦点需要设置 focusableInTouchMode为true。
- android:focusableInTouchMode 设置在Touch模式下View是否能取得焦点。
android:hapticFeedbackEnabled 设置长按时是否接受其他触摸反馈事件。这里模拟器没有试出效果,难道是多点触摸?找不到资料可以找找performHapticFeedback或HapticFeedback这个关键字的资料看看。 - android:isScrollContainer 设置当前View为滚动容器。这里没有测试出效果来,ListView/ GridView/ ScrollView根本就不用设置这个属性,而EdidText设置android:scrollbars也能出滚动条。
- android:keepScreenOn View在可见的情况下是否保持唤醒状态。
常在LinearLayout使用该属性,但是模拟器这里没有效果。 - android:longClickable 设置是否响应长按事件.
- android:minHeight 设置视图最小高度
- android:minWidth 设置视图最小宽度度
- android:nextFocusDown 设置下方 指定视图获得下一个焦点。焦点移动是基于一个在给定方向 查找最近邻居的算法。如果指定视图不存在,移动焦点时将报运行时错误。可以设置为imeOptions=actionDone,这样输入完即跳到下一个焦点。
- android:nextFocusLeft 设置左边 指定视图获得下一个焦点。
- android:nextFocusRight 设置右边 指定视图获得下一个焦点。
- android:nextFocusUp 设置上方 指定视图获得下一个焦点。
- android:saveEnabled 设置是否在窗口冻结时(如旋转屏幕)保存View的数据,默认为true,但是前提是你需要设置id才能自动保存,参见这里 。
- android:scrollX 以像素为单位设置水平方向滚动的的偏移值,在GridView中可看的这个效果。
- android:scrollY 以像素为单位设置垂直方向滚动的的偏移值
- android:scrollbarAlwaysDrawHorizontalTrack 设置是否始终显示水平滚动条
- android:scrollbarAlwaysDrawVerticalTrack 设置是否始终显示垂直滚动条。
- android:scrollbarDefaultDelayBeforeFade 设置N毫秒后开始淡化,以毫秒为单位。
android:scrollbarFadeDuration 设置滚动条淡出效果(从有到慢慢的变淡直至消失)时间,以毫秒为单位。Android2.2中滚动条滚动完之后会消失,再滚动又会出来,在1.5、1.6版本里面会一直显示着 - android:scrollbarSize 设置滚动条的宽度。
- android:scrollbarStyle 设置滚动条的风格和位置。设置值:insideOverlay、insideInset、outsideOverlay、outsideInset。
- android:scrollbarThumbHorizontal 设置水平滚动条的drawable(如颜色)。
- android:scrollbarThumbVertical 设置垂直滚动条的drawable(如颜色).
- android:scrollbarTrackHorizontal 设置水平滚动条背景(轨迹)的色drawable(如颜色)
- android:scrollbarTrackVertical
设置垂直滚动条背景(轨迹)的drawable注意直接设置颜色值如”android:color/white”将得出很难看的效果,甚至都不理解这个属性了,这里可以参见ApiDemos里res/drawable/ scrollbar_vertical_thumb.xml和scrollbar_vertical_track.xml,设置代码为:android:scrollbarTrackVertical =”@drawable/scrollbar_vertical_track” - android:scrollbars 设置滚动条显示。none(隐藏),horizontal(水平),vertical(垂直)。见下列代码演示使用该属性让EditText内有滚动条。 但是其他容器如LinearLayout设置了但是没有效果。
- android:soundEffectsEnabled 设置点击或触摸时是否有声音效果
- android:tag 设置一个文本标签。可以通过View.getTag()或 for with View.findViewWithTag()检索含有该标签字符串的View。但一般最好通过ID来查询View,因为它的速度更快,并且允许编译时类型检查。
- android:visibility 设置是否显示View。设置值:visible(默认值)。