Android学习-滚动视图ScrollView和HorizontalScrollView

一、简介:

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>
  • 运行截图:

三、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>
  • 运行截图(既能上下滚动也能左右滚动):

原文地址:https://www.cnblogs.com/ycmqaq/p/10035833.html

时间: 2024-08-28 22:21:27

Android学习-滚动视图ScrollView和HorizontalScrollView的相关文章

Android零基础入门第61节:滚动视图ScrollView

原文:Android零基础入门第61节:滚动视图ScrollView 前面几期学习了ProgressBar系列组件.ViewAnimator系列组件.Picker系列组件和时间日期系列组件,接下来几期继续来学习常见的其他组件. 一.ScrollView概述 从前面的学习有的同学可能已经发现,当拥有很多内容时屏幕显示不完,显示不全的部分完全看不见.但是在实际项目里面,很多内容都不止一个屏幕宽度或高度,那怎么办呢?那就需要本节学习的ScrollView来完成. 在默认情况下,ScrollView只是

Android 自学之滚动视图ScrollView

滚动视图ScrollView由FarmeLayout派生而出,他就是一个用于为普通组件添加垂直滚动条的组件:ScrollView里面最多包含一个组件,而ScrollView的作用就是为该组件添加一个垂直滚动条.(ScrollView的作用和JScrollPane非常相似,他们甚至不能被称为真正的容器.他们只是为其他的容器添加滚动条.)默认的情况下ScrollView只是为其他组件添加垂直滚动条,如果应用需要水平的滚动条,则可以借助于另一个组件:HorizontalScrollView来实现.Ho

制作滚动视图(ScrollView)

怎样判断是否应当使用滚动视图 所谓的滚动视图,是指一个可以滑动的视窗,视窗大小和位置固定不变,视窗内的内容用户可以通过手指滑动或者拖动滚动天来进行滚动浏览. 滚动视图的目的是为了解决同类内容过多,一个UI版面显示不下的情况.如果同类内容过多,一般可以采取设置多个页面,然后通过翻页浏览的方式来浏览,但是很明显,滚动视图会比翻页更方便,因为在移动上可以很方便地花瓶进行滚动,在PC上可以通过鼠标的滚动进行滚动. 当需要判断是否应该使用滚动视图制作UI时,可以遵循以下规律: (1)有很多同类内容一个版面

Unity3d UGUI 滚动视图ScrollView的使用教程之简易背包

Unity3d中,UGUI提供了 Scroll Rect.Grid Layout Group.Mask这三个组件,我们用来实现滚动视图,这里用一个简易的背包作为示例. 文章转自 http://blog.csdn.net/huutu/  星环游戏 http://www.thisisgame.com.cn 首先我们放置好背包的底层方框,如下图. 我们在红色的那一块,作为物品栏的滑动区域.   文章转自 http://blog.csdn.net/huutu/  星环游戏 http://www.this

Android学习-列表视图ListView

一.简介: ListView,列表视图,直接继承了AbsListView,是一个以垂直方式在项目中显示View视图的列表.ListView的数据项,来自一个继承了ListAdapter接口的适配器. 二.新建一个包listview并新建ListViewActivity.java活动: 12345678 public class ListViewActivity extends AppCompatActivity { @Override protected void onCreate(Bundle

Android学习-网格视图GridView

一.简介: GridView是一个以表格形式显示多张图片等组件.它是按照行列的方式来显示内容的,比如实现九宫格图,用GridView是首选. 二.代码块: 看过我上一篇博客的同学应该知道,一步一步全部步骤写出来是很费时间的,大概流程就那样,所以这次网格视图就直接上代码块了,步骤差不多. 在activity_main.xml中添加一个按钮: <Button android:id="@+id/btn_gridview" android:layout_width="match

Android中通过Java代码实现ScrollView滚动视图-以歌词滚动为例

场景 实现效果如下 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将布局改为LinearLayout,并通过android:orientation="vertical">设置为垂直布局,然后添加id属性,并设置内边距 <?xml version="1.0" encoding="utf-8"?> <L

Android搜索框/滚动视图/notification

1.搜索框(SearchView)的功能与用法 SearchView是搜索组件,其可以让用户在文本框内输入文件,并允许通过监听器监听用户输入.当用户输入完成后提交搜索,可通过监听器执行实际的搜索. 常用方法如下: ◆setIconfiedByDefault(boolean iconified):设置该搜索框默认是否自动缩小为图标; ◆setSubmitButtonEnabled(boolean enabled):设置是否显示搜索按钮; ◆setQueryHint(CharSequence hin

步步为营_Android开发课[25]_用户界面之ScrollView(滚动视图)

Focus on technology, enjoy life!-- QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305 主题:用户界面之ScrollView(滚动视图) - ScrollView滚动视图实例: activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayo