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

Android ListView 與 TextView、ImageView 垂直排列的佈局實現

     編輯:程式設計疑難解答

在 Android 開發中,經常需要在一個頁面內垂直排列 TextView、ImageView 和 ListView。本文給出標準的 LinearLayout 佈局方案,並說明關鍵屬性設置。

佈局 XML 示例

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="標題或提示文字" />

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

    <!-- ListView 高度設為 wrap_content 會導致只顯示一項,建議配合 weight 或固定高度 -->
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scrollbars="none" />

</LinearLayout>

關鍵點解析

  1. orientation="vertical":確保子視圖垂直排列。
  2. ListView 使用 layout_weight="1" + layout_height="0dp":佔據剩餘空間,避免與 ScrollView 嵌套衝突,也能完整顯示所有項目。
  3. 不要將 ListView 直接放入 ScrollView:會導致測量錯誤、只顯示第一項或滾動衝突。
  4. 自定義 Adapter:在 getView() 中複用 convertView,使用 ViewHolder 模式提升性能。

進階建議

  • 若列表項目較少且固定,可用 RecyclerView 替代 ListView,靈活性更高。
  • 需要列表上方固定 Header 時,可用 addHeaderView() 或 ConcatAdapter (RecyclerView) 實現。
  • 圖片加載建議使用 Glide/Picasso 異步加載,避免阻塞 UI 線程。

採用上述佈局結構,即可實現 TextView、ImageView、ListView 的垂直排列並保證列表正常滾動顯示。

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