概述

    scrollview也是一个容器,它是framelayout的子类,它的主要作用就是将超出物理屏幕的内容显示出来,(就是滚动条效果)scrollview提供垂直滚动,进而可将超出物理屏幕的内容显示出来。

      在一般情况下,可以将一个采用垂直方式布局组件的linearlayout作为scrolllayout容器的子组件,同时,在linearlayout容器中可以显示超出屏幕物理高度的内容。

练习

这么说有点抽象,然后我们现在实现完成一个调试板颜色的显示,效果如下:(可以往下滚动)

代码:

<?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">
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<view
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#07c2fb" />
<textview
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#07c2fb" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<view
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#c60426fd" />
<textview
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#c60426fd" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<view
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#032898" />
<textview
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#032898" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<view
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#021173" />
<textview
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#021173" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<view
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#504dae" />
<textview
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#504dae" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<view
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#3c60a6" />
<textview
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#3c60a6" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<view
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#282ea8" />
<textview
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#282ea8" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<view
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#273523" />
<textview
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="##5e7559" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<view
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#0e1e73" />
<textview
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#0e1e73" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<view
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#305bc8" />
<textview
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#305bc8" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<view
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#282ea8" />
<textview
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#282ea8" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<view
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#0b685e" />
<textview
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#0b685e" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<view
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#263b7e" />
<textview
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#263b7e" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<view
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#0a7ac5" />
<textview
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#0a7ac5" />
</linearlayout>
</linearlayout>
</scrollview>

      当然了scrollview只提供了垂直滚动条,若要使用水平滚动功能,则android提供了horizontalscrollview容器,horizontalscrollview容器可以提供水平滚动,它的使用方法与scrollview类似

horizontalscrollview:

<?xml version="1.0" encoding="utf-8"?>
<horizontalscrollview xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<linearlayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<linearlayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<view
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#2479ce" />
<textview
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#2479ce" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<view
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#c97a06" />
<textview
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#c97a06" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<view
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#68060e" />
<textview
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#68060e" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<view
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#271904" />
<textview
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#271904" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<view
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#673ab7" />
<textview
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#673ab7" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<view
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#1b5834" />
<textview
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#1b5834" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<view
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#680379" />
<textview
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#680379" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<view
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#043485" />
<textview
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#043485" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<view
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#798506" />
<textview
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#798506" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<view
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#b12e04" />
<textview
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#b12e04" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<view
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#a09006" />
<textview
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#a09006" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<view
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="2"
android:background="#920c02" />
<textview
android:layout_width="200px"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:text="#920c02" />
</linearlayout>
</linearlayout>
</horizontalscrollview>

 实现效果:

到此这篇关于android超详细讲解组件scrollview的使用的文章就介绍到这了,更多相关android scrollview内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!