日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android --- ListView之高度由 item 的个数决定,wrap_content有效

發布時間:2025/3/21 Android 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android --- ListView之高度由 item 的个数决定,wrap_content有效 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需要計算list中顯示條目的個數,然后在在代碼中設置listview的高度。

這里看看我怎么實現的
我們的布局代碼:

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@id/search_title_layout"android:orientation="vertical"><com.xis.read.view.ListViewForScrollViewandroid:id="@+id/lv_history"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/tv_clear_history"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/selector_common_gray_background"android:gravity="center"android:padding="10dp"android:text="@string/claer_history"android:textColor="@color/readpage_rose" /></LinearLayout>

上面的重要的只有一條ListView的高度用wrap_content

接下來我們只要自定義ListView就好了。這個自定義ListView也可以用于ScrollView中嵌套使用

package cn.xs.reader.view;import android.content.Context; import android.util.AttributeSet; import android.widget.ListView;/*** Created by Saud on 16/2/15.*/ public class ListViewForScrollView extends ListView {public ListViewForScrollView(Context context) {super(context);}public ListViewForScrollView(Context context, AttributeSet attrs) {super(context, attrs);}public ListViewForScrollView(Context context, AttributeSet attrs,int defStyle) {super(context, attrs, defStyle);}@Override/*** 重寫該方法,達到使ListView適應ScrollView的效果*/protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec, expandSpec);}}

總結

以上是生活随笔為你收集整理的Android --- ListView之高度由 item 的个数决定,wrap_content有效的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。