Android 自学之滚动视图ScrollView

滚动视图ScrollView由FarmeLayout派生而出,他就是一个用于为普通组件添加垂直滚动条的组件;ScrollView里面最多包含一个组件,而ScrollView的作用就是为该组件添加一个垂直滚动条。(ScrollView的作用和JScrollPane非常相似,他们甚至不能被称为真正的容器。他们只是为其他的容器添加滚动条。)默认的情况下ScrollView只是为其他组件添加垂直滚动条,如果应用需要水平的滚动条,则可以借助于另一个组件:HorizontalScrollView来实现。HorizontalScrollView和ScrollView的功能基本相似,只是前者添加水平滚动条,后者添加垂直滚动条。

通过一个程序来看看HorizontalScrollView和ScrollView的应用。在ScrollView中嵌套一个HorizontalScrollView,来为应用的界面同时添加水平、垂直的滚动条

layout/main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!-- 定义ScrollView,为里面的组件添加垂直滚动条 -->
 3 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent" >
 6     
 7     <!-- 定义HorizontalScrollView,为里面的组件添加水平滚动条 -->    
 8     <HorizontalScrollView 
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content">
11         <LinearLayout android:orientation="vertical"
12             android:layout_width="fill_parent"
13             android:layout_height="fill_parent">
14         <TextView android:layout_width="wrap_content"
15             android:layout_height="wrap_content" 
16             android:text="滚动视图滚动视图滚动视图"
17             android:textSize="30dp" />
18         <TextView android:layout_width="wrap_content"
19             android:layout_height="wrap_content" 
20             android:text="滚动视图滚动视图滚动视图"
21             android:textSize="30dp" />
22         <TextView android:layout_width="wrap_content"
23             android:layout_height="wrap_content" 
24             android:text="滚动视图滚动视图滚动视图"
25             android:textSize="30dp" />
26         <TextView android:layout_width="wrap_content"
27             android:layout_height="wrap_content" 
28             android:text="滚动视图滚动视图滚动视图"
29             android:textSize="30dp" />
30         <TextView android:layout_width="wrap_content"
31             android:layout_height="wrap_content" 
32             android:text="滚动视图滚动视图滚动视图"
33             android:textSize="30dp" />
34         <TextView android:layout_width="wrap_content"
35             android:layout_height="wrap_content" 
36             android:text="滚动视图滚动视图滚动视图"
37             android:textSize="30dp" />
38         <TextView android:layout_width="wrap_content"
39             android:layout_height="wrap_content" 
40             android:text="滚动视图滚动视图滚动视图"
41             android:textSize="30dp" />
42         <TextView android:layout_width="wrap_content"
43             android:layout_height="wrap_content" 
44             android:text="滚动视图滚动视图滚动视图"
45             android:textSize="30dp" />
46         <TextView android:layout_width="wrap_content"
47             android:layout_height="wrap_content" 
48             android:text="滚动视图滚动视图滚动视图"
49             android:textSize="30dp" />
50             
51         </LinearLayout>
52         
53     </HorizontalScrollView>
54     
55 </ScrollView>

上面界面显示和运行的效果:

posted on 2014-07-21 11:35  杨_静  阅读(1668)  评论(0编辑  收藏  举报

导航