Android中ListVIew高度自适应,解决ScrollView冲突问题以及Android表格
16lz
2021-12-04
效果图
类似格式,
listView_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/tieshu" android:layout_width="40dp" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="#000" android:gravity="center" android:textSize="12sp" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#000" /> <TextView android:id="@+id/usage" android:layout_width="40dp" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="#000" android:gravity="center" android:textSize="12sp" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#000" /> <TextView android:id="@+id/unit" android:layout_width="40dp" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center" android:textColor="#000" android:textSize="12sp" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#000" /> <TextView android:id="@+id/drugType" android:layout_width="40dp" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="#000" android:gravity="center" android:textSize="12sp" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#000" /> <TextView android:id="@+id/count" android:layout_width="40dp" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="#000" android:gravity="center" android:textSize="12sp" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#000" /> <TextView android:id="@+id/drugName" android:layout_width="60dp" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:textColor="#000" android:textSize="12sp" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#000" /></LinearLayout>
ListView 高度自适应代码 :
public static void setListViewHeightBasedOnChildren(ListView listView) {ListAdapter listAdapter = listView.getAdapter();if (listAdapter == null) {return;}int totalHeight = 0;for (int i = 0; i < listAdapter.getCount(); i++) {View listItem = listAdapter.getView(i, null, listView);listItem.measure(0, 0);totalHeight += listItem.getMeasuredHeight();}ViewGroup.LayoutParams params = listView.getLayoutParams();params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));listView.setLayoutParams(params);}
冲突:
如果嵌套在ScrollView中使用 listView无法自适应的话 请在setAdapter之后在调用自适应方法。
如果大家有什么好的 实现表格方法除了recyclerview 请推荐 谢谢。。
更多相关文章
- android 2.2+ 完全退出程序的方法
- android 设置壁纸几种方法
- Android文件存储
- Anfdroid网络编程方法
- android 全面讲解BroadCastReceiver
- Android蓝牙开发浅析
- Android休眠唤醒机制简介(二)
- Android学习札记17:ImageView中的setImageBitmap()方法
- android输入法全屏问题