• 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实现上次登录功能
    TRUNCATE 不能引发触发器
    unslider点导航不显示错误
    jquery插件中使用ajax并且获取使用插件的对象
    jquery插件函数传参错误
    jquery插件获取事件类型
    线程安全的 stack
    不要在锁的作用域之外通过指针或引用传递要保护的数据
    通过打包 accumulate 实现多线程版本的 accumulate
  • 原文地址:https://www.cnblogs.com/linhaostudy/p/14120799.html
Copyright © 2020-2023  润新知