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

Android ListView getView 多次呼叫導致 PopupWindow 顯示異常空白區域?根因在佈局層級

     編輯:程式設計綜合問答

現象

自訂 PopupWindow 內含 ListView(5 項),底部出現大片空白。Log 顯示 getView 被呼叫多次,懷疑是重複渲染導致。

真相:getView 多次呼叫是常態,空白源於 ListView 佈局高度計算

  • ListView 在 measure 階段會多次呼叫 getView 取得子視圖測量高度,不等於實際渲染重複。
  • 空白區域通常因 **父佈局(LinearLayout/RelativeLayout)高度設為 wrap_content**,但 ListView 在 ScrollView/非固定高度容器中無法正確測量,導致預留過多空間。

解決方案:簡化父佈局,給 ListView 固定或 match_parent 高度

<!-- 錯誤:外層 wrap_content,ListView 無法測量 -->
<LinearLayout android:layout_height="wrap_content">
    <ListView android:layout_height="wrap_content" />
</LinearLayout>

<!-- 正確:外層固定高度或 match_parent,ListView match_parent -->
<LinearLayout android:layout_height="300dp">  
    <ListView android:layout_height="match_parent" />
</LinearLayout>

參考資源

詳細原理見:ListView measure 過程與 getView 多次呼叫分析

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