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

Android ScrollView 軟鍵盤彈出無法滾動:佈局結構調整

     編輯:程式設計綜合問答

問題現象

android:windowSoftInputMode="adjustResize|stateHidden" 已設定,但鍵盤彈出時 ScrollView 內容不滾動,底部被遮擋。

根本原因

佈局結構中 ScrollView 為 RelativeLayout 直接子視圖,且未約束在底部 Button 上方。adjustResize 需要根佈局可縮放,但 RelativeLayout 高度固定 match_parent,導致縮放無效。

解決方案

<RelativeLayout ...>
    <Button
        android:id="@+id/bottomBtn"
        android:layout_alignParentBottom="true" ... />

    <ScrollView
        android:layout_above="@id/bottomBtn"  // 關鍵:約束在 Button 上方
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">
        <!-- 內容 -->
    </ScrollView>
</RelativeLayout>

關鍵點

  • ScrollView 加上 android:layout_above="@id/bottomBtn"
  • 底部固定元素(Button)需先定義 id 並 alignParentBottom
  • ScrollView 高度用 wrap_content 或 match_parent 配合約束均可

替代方案

使用 ConstraintLayout 作為根佈局,配合 app:layout_constraintBottom_toTopOf="@id/bottomBtn" 更靈活。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved