在一个Activity中如果添加的组件过多,布局超出屏幕的的宽度或高度的时候,有些组件就无法显示,为了解决这个问题,可以用滚动视图ScrollView来解决。
滚动视图ScrollView由Framlayout派生而来,他就是为普通组件添加滚动条的组件。ScrollView里最多只能包含一个组件,而ScrollView的作用就是为给组件添加滚动条。而这和最初说的组件数量过多冲突,我们只要为所有组件最外层的布局组件添加ScrollView就好了。
默认情况下,ScrollView只能为其他组件添加垂直滚动条,如果应用需要实现添加水平滚动条,则需要借助另一个滚动视图:HoriScrollView来实现。ScrollView与HoriScrollView的功能基本类似,只是前者添加垂直滚动条,后者添加水平滚动条。下面看一个具体的例子。
ScrollView应用示例:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 定义HorizontalScrollView,为里面的组件添加水平滚动条 -->
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="滚动视图滚动视图滚动视图"
android:textSize="30dp" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="滚动视图滚动视图滚动视图"
android:textSize="30dp" />
……………
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
注:代码20行处省略了N个TextView的定义。为了实现滚动视图的效果,读者可以多建几个。
程序运行结果:
转载于:https://blog.51cto.com/shaotao/1279161