欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android学习-滚动视图ScrollView和HorizontalScrollView

程序员文章站 2022-07-02 11:54:29
一、简介: ScrollView,通过官方文档的继承关系可以看出,它继承自FrameLayout,所以它是一种特殊类型的FrameLayout,因为它可以使用用户滚动显示一个占据的空间大于物理显示的视图列表。值得注意的是,ScrollView只能包含一个子视图或视图组,在实际项目中,通常包含的是一个 ......

一、简介:

scrollview,通过官方文档的继承关系可以看出,它继承自framelayout,所以它是一种特殊类型的framelayout,因为它可以使用用户滚动显示一个占据的空间大于物理显示的视图列表。值得注意的是,scrollview只能包含一个子视图或视图组,在实际项目中,通常包含的是一个垂直的linearlayout。

二、scrollview代码块:

  • 在activity_main.xml中添加一个超出页面范围的按钮:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <button
        android:id="@+id/btn_textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="textview"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button"
        android:textallcaps="false"/>
    
    <button
        android:id="@+id/btn_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="edittext"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_radiobutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="radiobutton"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="checkbox"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_imageview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="imageview"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="listview"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_gridview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="gridview"
        android:textallcaps="false"/>

    <button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test"
        android:textallcaps="false"
        android:layout_margintop="300dp"/>

</linearlayout>
  • 更改为scrollview布局(scrollview下子元素只能有一个):
<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="wrap_content"
    >

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

    <button
        android:id="@+id/btn_textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="textview"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="edittext"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_radiobutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="radiobutton"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="checkbox"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_imageview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="imageview"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="listview"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_gridview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="gridview"
        android:textallcaps="false"/>

    <button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test"
        android:textallcaps="false"
        android:layout_margintop="300dp"/>

    </linearlayout>
</scrollview>
  • 运行截图:
    Android学习-滚动视图ScrollView和HorizontalScrollView

三、horizontalscrollview代码块:

  • 在原有scrollview基础上写入horizontalscrollview(horizontalscrollview下子元素只能有一个):
<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="wrap_content"
    >

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

    <button
        android:id="@+id/btn_textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="textview"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="edittext"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_radiobutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="radiobutton"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="checkbox"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_imageview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="imageview"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="listview"
        android:textallcaps="false"/>

    <button
        android:id="@+id/btn_gridview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="gridview"
        android:textallcaps="false"/>

    <horizontalscrollview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <linearlayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <button
                android:layout_width="300dp"
                android:layout_height="300dp"
                android:text="test"
                android:textallcaps="false"/>
            <button
                android:layout_width="300dp"
                android:layout_height="300dp"
                android:text="test"
                android:textallcaps="false"/>
            <button
                android:layout_width="300dp"
                android:layout_height="300dp"
                android:text="test"
                android:textallcaps="false"/>
            <button
                android:layout_width="300dp"
                android:layout_height="300dp"
                android:text="test"
                android:textallcaps="false"/>
        </linearlayout>
    </horizontalscrollview>

    </linearlayout>
</scrollview>
  • 运行截图(既能上下滚动也能左右滚动):
    Android学习-滚动视图ScrollView和HorizontalScrollView