网站建设是啥工作,wordpress 相关文章,推广之家app,如何设置标签 wordpress目录一、简介二、效果图三、代码实现一、简介 在Android开发的过程中有的时候我们需要手动计算ListView的高度#xff0c;比如说#xff0c;ScrollView中嵌套ListView的时候#xff0c;我们就需要手动精确计算ListView的高度了。
如果ListView的Item高度是固定的话还好计算…
目录一、简介二、效果图三、代码实现一、简介 在Android开发的过程中有的时候我们需要手动计算ListView的高度比如说ScrollView中嵌套ListView的时候我们就需要手动精确计算ListView的高度了。
如果ListView的Item高度是固定的话还好计算一些我们可以直接使用Item的条数 * Item的固定高度来计算但是如果Item的高度随着内容的变化而变化那么该如何计算呢
下面我们就开始说说如何精确计算ListView的高度吧。
二、效果图 先看下界面效果 从效果图中我们可以看到
红色背景的是Item蓝色背景的是ListView的dividerHeight的高度同时我们也设置了ListView的paddingTop和paddingBottom值。
三、代码实现 下面我们就直接上代码
1、Item的布局文件list_item.xml
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:orientationverticalandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentTextViewandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:idid/txt_item_infoandroid:gravitycenterandroid:textColor#ffffffandroid:padding20dpandroid:textSize18spandroid:lineSpacingExtra10dpandroid:text测试一android:backgroundcolor/colorAccent//LinearLayoutItem布局文件中就定义了一个TextViewTextView的高度随着内容的变化而变化。
2、ListView界面的布局文件activity_main.xml
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticaltools:context.MainActivityTextViewandroid:layout_widthmatch_parentandroid:layout_height50dpandroid:gravitycenterandroid:background#30B8E3android:textColor#ffffffandroid:textSize18spandroid:text动态计算ListView高度/Buttonandroid:layout_widthmatch_parentandroid:layout_height50dpandroid:idid/btn_addandroid:text添加Item/ScrollViewandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentLinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationverticalListViewandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:idid/listviewandroid:dividercolor/colorPrimaryDarkandroid:dividerHeight10dpandroid:paddingTop10dpandroid:paddingBottom10dpandroid:cacheColorHint#00000000android:listSelector#00000000android:background#ffffffandroid:orientationvertical/ListViewTextViewandroid:layout_widthmatch_parentandroid:layout_height50dpandroid:background#30B8E3android:textColor#ffffffandroid:gravitycenterandroid:text检测ListView高度是否精确//LinearLayout/ScrollView/LinearLayout这里我们设置了ListView的dividerHeight、paddingTop、paddingBottom。 3、ListView高度计算 布局文件准备好后我们就来看下最关键的地方动态计算ListView的高度这里我们只贴出计算ListView高度的代码 public void setListViewHeight(ListView listview){ListAdapter adapter listview.getAdapter();if(adapter null){return;}int totalHeight 0;// 计算ListView的宽度int listViewWidth ((Activity)mContext).getWindowManager().getDefaultDisplay().getWidth();int widthSpec View.MeasureSpec.makeMeasureSpec(listViewWidth, View.MeasureSpec.AT_MOST);for(int i0;iadapter.getCount();i){View view adapter.getView(i, null, listview);// 这里的第一个参数必须使用widthSpec// 如果使用0的话无法计算出随内容变化而变化的Item的真正高度值view.measure(widthSpec, 0);totalHeight view.getMeasuredHeight();}int dividerHeight listview.getDividerHeight() * (adapter.getCount() - 1);totalHeight dividerHeight;Log.i(ListViewHeight, ListView DividerHeight : dividerHeight);int paddingHeight listview.getPaddingTop() listview.getPaddingBottom();totalHeight paddingHeight;Log.i(ListViewHeight, ListView PaddingHeight : paddingHeight);Log.i(ListViewHeight, ListView TotalHeight : totalHeight);ViewGroup.LayoutParams layoutParams listview.getLayoutParams();layoutParams.height totalHeight;listview.setLayoutParams(layoutParams);this.refresh();}其中最关键的地方就是下面这几行代码 // 计算ListView的宽度int listViewWidth ((Activity)mContext).getWindowManager().getDefaultDisplay().getWidth();int widthSpec View.MeasureSpec.makeMeasureSpec(listViewWidth, View.MeasureSpec.AT_MOST);// 这里的第一个参数必须使用widthSpec// 如果使用0的话无法计算出随内容变化而变化的Item的真正高度值view.measure(widthSpec, 0);完整代码已上传至Github动态计算ListView高度