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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM