zoukankan      html  css  js  c++  java
  • Android的ScrollView简单使用实例(附Demo)

    1.垂直滚动:Scroll

    新建一个应用程序:

    在MainActivity的布局文件上做个实验,现在设置了按钮1和按钮2后还剩下一些空位:

    image

    再设置一个按钮3让他超出屏幕之外:

    image

    现在去运行程序,是滑动不了, 看不到按钮3的。

    image

    应该如何设置呢?

    1.改变这个布局文件的根布局:把根布局改成:ScrollView

    注意:ScrollView的子元素只能有一个,所以得增加一个LinearLayout布局,把其他按键放在这个LinearLayout中,那么ScrollViewd的子元素就只有一个LinearLayout了,而LinearLayout的子元素不限制。

    代码如下:

    <?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"
        android:padding="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
            <Button
                android:id="@+id/IVButton_Id"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="跳转到ImageView"
                android:textSize="20dp"
                android:textAllCaps="false"/>
            <Button
                android:id="@+id/LVButton_Id"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="跳转到ListView"
                android:textSize="20dp"
                android:textAllCaps="false"/>
            <Button
                android:id="@+id/GVButton_Id"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="跳转到GridView"
                android:textSize="20dp"
                android:textAllCaps="false"/>
     
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="按钮1"
                android:textSize="20dp"
                android:textAllCaps="false"
                android:layout_marginTop="100dp"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="按钮2"
                android:textSize="20dp"
                android:textAllCaps="false"
                android:layout_marginTop="100dp"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="按钮3"
                android:textSize="20dp"
                android:textAllCaps="false"
                android:layout_marginTop="300dp"/>
        </LinearLayout>
     
    </ScrollView>
    

    运行程序,现在就可以向下滚动,看到按钮3了:

    image

    2.水平滚动:HorizontalScrollView

    在LinearLayout里新建一个HorizontalScrollView,同样他的子元素只能有一个

    image

    所以在HorizontalScrollView布局中再加一个子布局LinearLayout,且LinearLayout为水平方向:

    image

    代码如下:

    <?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"
        android:padding="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
            <Button
                android:id="@+id/IVButton_Id"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="跳转到ImageView"
                android:textSize="20dp"
                android:textAllCaps="false"/>
            <Button
                android:id="@+id/LVButton_Id"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="跳转到ListView"
                android:textSize="20dp"
                android:textAllCaps="false"/>
            <Button
                android:id="@+id/GVButton_Id"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="跳转到GridView"
                android:textSize="20dp"
                android:textAllCaps="false"/>
     
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="按钮1"
                android:textSize="20dp"
                android:textAllCaps="false"
                android:layout_marginTop="100dp"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="按钮2"
                android:textSize="20dp"
                android:textAllCaps="false"
                android:layout_marginTop="160dp"/>
            <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="200dp"
                        android:layout_height="300dp"
                        android:text="按钮3" />
                    <Button
                        android:layout_width="200dp"
                        android:layout_height="300dp"
                        android:text="按钮4" />
                </LinearLayout>
            </HorizontalScrollView>
     
        </LinearLayout>
     
    </ScrollView>
    

    运行应用程序,因为外面还嵌套了一层ScrollView所以能垂直滚动和水平滚动:

    image

  • 相关阅读:
    PHP输出中文乱码的问题(转)
    phpmyadmin导出数据库为什么是php文件
    phpmyadmin登陆提示#2002 无法登录 MySQL 服务器和设置自增
    phpMyAdmin配置及 错误 缺少 mysqli 扩展。请检查 PHP 配置
    利用eclipse开发php<转>
    apache 2.4 You don't have permission to access / on this server
    (转)如果“打开方式”里面没有想要的打开方式,怎样创建一种文件打开方式?
    (转)安装 Apache 出现 <OS 10013> 以一种访问权限不允许的方式做了一个访问套接字的尝试
    关于ISAPI和CGI限制,这个要设为允许
    Sqlserver数据库日志太大如何快速删除
  • 原文地址:https://www.cnblogs.com/linhaostudy/p/14120799.html
Copyright © 2011-2022 走看看