Android中的ScrollView控件

滚动条,它更像一种布局,但ScrollView只能有一个孩子,只有一个孩子怎么滚动呢,所以需要先嵌套一个布局,如线性布局,这样就可以包含多个内容,内容过多就可以滚动。ScrollView只能设置垂直滚动,水平滚动有对应的HorizontalScrollView,同样它只能设置水平滚动。下面为方便测试,进重复显示相同内容。

布局XML代码

<ScrollView 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:padding="10dp"
    android:background="#38ff88"
    android:scrollbars="vertical">
    <LinearLayout 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:orientation="vertical">
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:textColor="#ff0746"
            android:textSize="50sp"
            android:gravity="center"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="#0100fd"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:textColor="#ff0746"
            android:textSize="50sp"
            android:gravity="center"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="#0100fd"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:textColor="#ff0746"
            android:textSize="50sp"
            android:gravity="center"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="#0100fd"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:textColor="#ff0746"
            android:textSize="50sp"
            android:gravity="center"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="#0100fd"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:textColor="#ff0746"
            android:textSize="50sp"
            android:gravity="center"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="#0100fd"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:textColor="#ff0746"
            android:textSize="50sp"
            android:gravity="center"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="#0100fd"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:textColor="#ff0746"
            android:textSize="50sp"
            android:gravity="center"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="#0100fd"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:textColor="#ff0746"
            android:textSize="50sp"
            android:gravity="center"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="#0100fd"/>
	</LinearLayout>
</ScrollView>
效果图如下

  可以看到,右边出现的灰色竖线就是滚动条显示的位置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值