问题描述
我在一个可滚动视图中有多个列表视图,所以我使用了一个函数来计算列表视图的高度并设置它以便列表视图不再可滚动。
public static void setHeightListView(ListView listView) {
Adapter adapter = listView.getAdapter();
int UNBOUNDED = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int height = listView.getDividerHeight() * (listView.getCount() - 1);
for (int i = 0; i < adapter.getCount(); ++i) {
View child = adapter.getView(i,null,listView);
child.measure(UNBOUNDED,UNBOUNDED);
height += child.getMeasuredHeight();
}
ViewGroup.LayoutParams listViewParams = listView.getLayoutParams();
listViewParams.height = height;
listView.requestLayout();
}
到目前为止,这个函数运行良好,但它没有在我的适配器中的 getView() 中计算出正确的高度:
convertView = inflater.inflate(R.layout.activity_frame_no_rounded_corners,null);
activity_frame_no_rounded_corners.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="wrap_content"
android:orientation="horizontal"
android:paddingHorizontal="10dp"
android:focusable="false"
android:background="@drawable/textview_white">
<LinearLayout
android:layout_width="match_parent"
android:focusable="false"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageview_modify"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:focusable="false"
android:gravity="center_vertical"
android:src="@drawable/ic_baseline_remove_circle_24" />
<LinearLayout
android:layout_width="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:focusable="false"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textview_key"
android:text="Key"
android:textSize="15dp"
android:padding="7dp"
android:textColor="#000000"
android:focusable="false"
android:layout_width="0dp"
android:layout_weight="0.4"
android:layout_height="wrap_content"
android:hint="Vlue"/>
<View
android:id="@+id/vertical_bar_devider"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#dcdcdc"/>
<EditText
android:id="@+id/edittext_value"
android:text="Value"
android:textSize="15dp"
android:padding="7dp"
android:textColor="#000000"
android:layout_width="0dp"
android:layout_weight="0.6"
android:layout_height="wrap_content"
android:hint="Value"
android:background="@drawable/textview_white"
android:drawableRight="@drawable/ic_baseline_arrow_forward_ios_24_gray"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
如果我使用不同的 xml,那么它会计算正确的高度。此外,如果我从视图中删除权重,它也会计算正确的高度(但我需要对齐权重,我找不到解决方法)。
您可以在屏幕截图中看到我拥有的不同列表视图(一种用于姓名,一种用于 dob,一种用于等级,一种用于电子邮件,一种用于 gmc 编号,一种用于专业)。
出于测试目的,它们没有任何边距,因此它们应该一个接一个。
如您所见,电子邮件列表视图的高度计算正确,但我的函数在其余列表中失败。如果我将“GMC 编号”更改为“电子邮件”,则该函数还会为 gmc 无列表视图计算正确的高度(不知道为什么,这只是一个观察)。
我想我的问题与重量有关,但我不知道如何解决。
先谢谢你!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)