Android 页面 滚动

这篇博客介绍了Android中ScrollView的使用,它作为一个容器,允许显示超出屏幕的内容并提供垂直滚动条。同时,还提到了HorizontalScrollView用于实现水平滚动。文章通过示例代码展示了如何在布局中使用这两个组件,并提供了监听按钮点击实现滚动到顶部和底部的方法。

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

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

1.1.2首先给按钮设置id,其次页面的内容必须长,否则跳转不到另一个页面
1.1.3用到了 <scrollView></scrollView>



2.ScrollView只提供了垂直滚动条,若要使用水平滚动功能,则Android提供了HorizontalScrollView容器,HorizontalScrollView容器可以提供水平滚动,它的使用方法与ScrollView类似

这是页面代码  

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btn_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="跳转底部 " />

    <Button
        android:id="@+id/btn_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="返回顶部" />
</LinearLayout>

<ScrollView
    android:id="@+id/sv"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="顶部" />
    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="文本2" />

    <TextView
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本10" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="底部" />
    </LinearLayout>

</ScrollView>

这是后端代码
// 对于监听的处理
@Override
public void onClick(View v) {
    if (v.getId() == R.id.btn_top) {
        // 返回顶部
        scrollView.fullScroll(ScrollView.FOCUS_UP);
    } else if (v.getId() == R.id.btn_bottom) {
        // 跳转底部
        scrollView.fullScroll(ScrollView.FOCUS_DOWN);
    } else {

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值