Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 為 Activity 加入水平與垂直滾動條的方法

編輯:關於 Android 程式設計

當 Activity 的內容超出螢幕顯示範圍時,就需要靠滾動條來瀏覽全部內容。若同時需要垂直與水平方向的滾動,可以透過 ScrollView 嵌套 HorizontalScrollView 來實現。

 <ScrollView xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" > <HorizontalScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_height" > </LinearLayout> </HorizontalScrollView> </ScrollView> 

結構很單純:最外層是 ScrollView,其子標籤是 HorizontalScrollView,再把你要滾動的內容布局放進最內層即可。

要特別注意:ScrollView 與 HorizontalScrollView 底下都只能嵌入一個子標籤。若需要放多個元件,請先用一個容器(例如 LinearLayout)把它們包起來,再放進滾動檢視中,否則會編譯出錯。

掌握這個嵌套結構,就能輕鬆為 Activity 加上雙向滾動能力,讓超出螢幕的內容都能完整呈現。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved