前面几期学习了ProgressBar系列组件、ViewAnimator系列组件、Picker系列组件和时间日期系列组件,接下来几期继续来学习常见的其他组件。
一、ScrollView概述
从前面的学习有的同学可能已经发现,当拥有很多内容时屏幕显示不完,显示不全的部分完全看不见。但是在实际项目里面,很多内容都不止一个屏幕宽度或高度,那怎么办呢?那就需要本节学习的ScrollView来完成。
在默认情况下,ScrollView只是为其他组件添加垂直滚动条,如果应用需要添加水平滚动条,则可借助于另一个滚动视图HorizontalScrollView来实现。ScrollView与HorizontalScrollView的功能基本相似,只是前者添加垂直滚动条,后者添加水平滚动条。
ScrollView由FrameLayout派生而出,它就是一个用于为普通组件添加滚动条的组件。 ScrollView里最多只能包含一个组件,而ScrollView的作用就是为该组件添加垂直滚动条。
ScrollView支持的XML属性如下:
ScrollView的几个常用方法有:
二、ScrollView示例
接下来通过一个简单的示例程序来学习ScrollView的使用。
继续使用WidgetSample工程的advancedviewsample模块,在app/main/res/layout/目录下创建scrollview_layout.xml文件,在其中填充如下代码片段:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:fillViewport="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@drawable/image" />
<Button
android:id="@+id/more_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="more" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ScrollView"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/description"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</ScrollView>
其中description为定义的字符串,由于内容较多,此处不在给出。
运行程序,可以看到下图所示界面效果,界面可以上下滚动。
关于ScrollView先学到这里,更多用法建议自己多加练习。