Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 應用開發入門(二十八):ScrollView 與 HorizontalScrollView 滾動容器解析

編輯:Android 開發實例

本文主要講解 ScrollView 與 HorizontalScrollView 這兩個滾動容器。顧名思義,它們分別負責垂直與水平方向的滾動,而被滾動的內容就是包在裡面的 View。

這兩個容器本身的 API 並不多,真正的重點在於「什麼時候該用、什麼時候絕對不能用」。我會把使用方式與這些注意事項一起說明,最後用 Demo 驗證效果。

ScrollView

從官方文件的繼承關係可以看到,ScrollView 繼承自 FrameLayout,本質上是一種特殊的 FrameLayout——它能讓使用者滾動瀏覽一個實際尺寸大於螢幕可見範圍的視圖。

它有個硬性限制:只能包含一個子視圖。實務上這個子視圖通常就是一個垂直排列的 LinearLayout,再把要滾動的內容全部塞進去。

兩個不能混用的情況

這一節是整篇最需要記住的部分。ScrollView 的本質是「滾動視圖的容器」,因此凡是自己已經處理了滾動的控件,都不能放進來:

  • 不要和 ListView 混用。ListView 本身已經完整處理了垂直方向的滾動與視圖復用。把它塞進 ScrollView,兩者的滾動會互相打架,而且 ListView 的復用機制也會失效,效能反而更差。
  • 不要和多行 EditText 混用。多行 EditText 自帶滾動能力,放進 ScrollView 之後,它自身的滾動效果會失效。

一句話總結:ScrollView 管的是「頁面整體的滾動」,不是「某個列表的滾動」。

HorizontalScrollView

在 Android 平台下與 ScrollView 對應的还有一個 HorizontalScrollView,思路完全相同,只是方向改成水平。理解了 ScrollView,HorizontalScrollView 幾乎不需要另外學習。

Demo:垂直滾動

ScrollView 基本上就是一個布局容器,沒有太多自己的方法或屬性需要特別講解。直接看範例最有效率。以下範例放入十張圖片,需先將圖片置於 res/drawable-hdpi 目錄。

布局代碼:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- ScrollView 只能有一個直接子節點 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="垂直滾動視圖"
            android:textSize="30dp" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp1" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp2" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp3" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:maxLines="2" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp4" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp5" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp6" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp7" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp8" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp9" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp10" />

    </LinearLayout>

</ScrollView>

注意那個內層 LinearLayout 的高度必須是 wrap_content 而不是 match_parent——如果寫成 match_parent,內容會被壓縮到螢幕高度,滾動也就無從發生。這是初學者最常踩的一個坑。

改成水平滾動有多簡單

HorizontalScrollView 的邏輯與 ScrollView 如出一轍,唯一差別是支援水平滾動。要從上面的例子改過去,只需兩個動作:

  1. 把外層的 ScrollView 換成 HorizontalScrollView。
  2. 把內層 LinearLayout 的 android:orientation 改成 horizontal。

因為沒有任何新的技術含量,這裡就不再貼一遍完整的 Demo 代碼了。

總結:ScrollView 還是 ListView?

在現在的 Android 開發中,多數需要滾動的場景——例如滑動瀏覽新聞串——都會直接用 ListView 或 RecyclerView 來裝載資料,因為它們天生就是為「大量、可變長度」的資料而設計的。

但 ScrollView 仍有它無法被取代的位置。典型場景是設定頁:內容是固定的一批表單元件(帳號欄位、開關、版本資訊、關於我們),數量不隨資料變動,但整體高度又會超過一個螢幕。這種情況用 ScrollView 把表單包起來就是最乾淨的解法。

所以判斷標準可以濃縮成一句話:

  • 內容是動態、數量不定的清單 → 用 ListView / RecyclerView。
  • 內容是固定、版面明確的表單或圖文 → 用 ScrollView 包起來即可。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved