欢迎光临散文网 会员登陆 & 注册

Android开发学习教程(20)- 滚动控件ScrollView的用法

2023-01-28 14:54 作者:考研保研直通车  | 我要投稿

—— 与其去讨好别人,不如武装自己,与其去听风听雨,不如昂首的走出去。

ScrollView是什么

ScrollView是一种特殊的布局。当ScrollView嵌套的内容大于他本身的size的时候,ScrollView会自动添加滚动条,并可以竖直滑动。

ScrollView有什么用

当ScrollView嵌套的内容大于他本身的size的时候,ScrollView会自动添加滚动条,并可以竖直滑动。

ScrollView怎么用

1. 布局中加入ScrollView控件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:text="textview1" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:text="textview2" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:text="textview3" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:text="textview4" />
    </LinearLayout>
</ScrollView>

运行效果:

1. ScrollView的直接子View只能有一个。也就是说如果你要使用很复杂的视图结构,必须把这些视图放在一个标准布局里,如LinearLayout、RelativeLayout等。

2. 你可以使用layout_width和layout_height给ScrollView指定大小。

3. ScrollView只用来处理需要滚动的不规则视图的组合。大批量的列表数据展示可以使用ListView、GridView或者RecyclerView。

4. ScrollView和ListView之类的嵌套使用时会有滑动冲突。不到不得已不要使用。

5. ScrollView只支持竖直滑动,水平滑动使用HorizontalScrollView。

6. ScrollView的android:fillViewport属性定义了是否可以拉伸其内容来填满viewport。你可以可以调用方法setFillViewport(boolean)来达到一样的效果。

源码链接:https://yunjunet.cn/876795.html

Android开发学习教程(20)- 滚动控件ScrollView的用法的评论 (共 条)

分享到微博请遵守国家法律