首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android 佈局超出螢幕實現可拖動查看:ScrollView 使用指南

     編輯:程式設計綜合問答

核心方案

將整個佈局的根視圖包裹在 <ScrollView> 中,即可實現垂直滾動;水平滾動用 <HorizontalScrollView>。

基本結構

<ScrollView
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"> <!-- 隱藏滾動條 -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!-- 這裡放置原本的所有內容 -->

    </LinearLayout>
</ScrollView>

關鍵屬性

屬性說明
android:scrollbars="none"隱藏滾動條(垂直/水平)
android:fillViewport="true"內容不滿螢幕時也撐滿,避免底部空白
android:overScrollMode="never"禁用過度滾動邊緣光暈效果

注意事項

  • ScrollView 只能有一個直接子視圖,通常用 LinearLayout/ConstraintLayout 包裹所有內容。
  • 嵌套 ScrollView 與 RecyclerView/ListView 會導致滾動衝突,需使用 NestedScrollView + recyclerView.setNestedScrollingEnabled(false)。
  • 性能:長列表請改用 RecyclerView,不要把大量項目放入 ScrollView。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved