Android --- ListView之高度由 item 的个数决定,wrap_content有效
生活随笔
收集整理的這篇文章主要介紹了
Android --- ListView之高度由 item 的个数决定,wrap_content有效
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
需要計算list中顯示條目的個數,然后在在代碼中設置listview的高度。
這里看看我怎么實現的
我們的布局代碼:
上面的重要的只有一條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有效的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android --- 如何更改应用程序
- 下一篇: Android --- 很好用的时间选择