Android学习之路--布局二

本文详细介绍了ScrollView和HorizontalScrollView在Android应用开发中的使用方法及注意事项。通过实例展示了如何利用ScrollView解决页面组件过多导致的显示问题,并强调了子布局的限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ScrollView

使用

HorizontalScrollView

使用


ScrollView的使用:在某些页面布局下,如果里面的组件过多,而现有布局又不支持滑动,导致后面的组件看不到,那么我们可以用ScrollView来进行填充。例如:

<?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="match_parent"
        android:orientation="vertical">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="第一排" />

            <TableRow>

                <TextView
                    android:id="@+id/tv_one"
                    android:layout_width="match_parent"
                    android:layout_height="500dp"
                    android:background="#bd0708"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="我是第一个组件" />

                <TextView
                    android:id="@+id/tv_two"
                    android:layout_width="match_parent"
                    android:layout_height="500dp"
                    android:background="#666"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="我是第二个组件" />

                <TextView
                    android:id="@+id/tv_three"
                    android:layout_width="match_parent"
                    android:layout_height="500dp"
                    android:background="#999"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="我是第二个组件" />
            </TableRow>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="第二排" />

            <TableRow>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="500dp"
                    android:background="#bd0708"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="我是第一个组件" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="80dp"
                    android:background="#666"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="我是第二个组件" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="80dp"
                    android:background="#999"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="我是第二个组件" />
            </TableRow>
        </TableLayout>
    </LinearLayout>
</ScrollView>

注意:在使用ScrollView时,里面子布局只能有一个Layout包括起来。否则会报:ScrollView can host only one direct child错误。

同理HorizontalScrollView也是一样。

这一篇顺便测试一下目录生成。

如果布局还有些疑问的话,欢迎请看 Android 学习之路--布局篇


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值