日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/3/21 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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有效的全部內容,希望文章能夠幫你解決所遇到的問題。

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