安卓基础(九)

滚动吧!

目标人群:没有基础的安卓初学者
知识点:ScrollView的使用
目标:当控件超出页面显示范围时,页面可以滑动

简介

  • ScrollView的使用

  • HorizontalScrollView的使用

正文

1.首先要说明的是ScrollView是一个layout,添加一个ScrollView将activity_main.xml内部的元素包裹起来,代码如下。

<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:orientation="vertical"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textView"
            android:text="@string/hello_world"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="随便说点什么吧?"
            android:id="@+id/editText" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="800dp"
            android:text="下一页"
            android:onClick="goHello"
            android:id="@+id/button" />

    </LinearLayout>
</ScrollView>
  • ScrollView 的内部只可以有一个子元素,在本页面中为LinearLayout

  • 由于是纵向滑动,LinearLayout的android:layout_height值应为wrap_content

  • 当控件的高度超过屏幕高度时,ScrollView 的效果才可被观察到,因此Button控件的高度被设置成了一个较大的值

2.如果当控件的宽度超过屏幕宽度时,我们需要左右滑动页面,此时需要用到HorizontalScrollView,代码如下

<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:orientation="vertical"
        android:layout_height="fill_parent">

        <TextView
            android:id="@+id/textView"
            android:text="@string/hello_world"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="随便说点什么吧?"
            android:id="@+id/editText" />

        <Button
            android:layout_width="800dp"
            android:layout_height="wrap_content"
            android:text="下一页"
            android:onClick="goHello"
            android:id="@+id/button" />

    </LinearLayout>
</HorizontalScrollView>
  • 同上一页面相比,LinearLayout的android:layout_height应为fill_parent,而android:layout_width应为wrap_content

  • Button控件的宽度被改为了800dp

3.如果觉得滚动条的样子不是很美观,可以将ScrollView中添加android:scrollbars=”none”属性,实现
隐藏滚动条的作用

扩展阅读

  1. 如何理解滑动?
  2. ScrollView的官方API
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值