首页 > 解决方案 > 在 Android Studio 中测量高度无法正常工作

问题描述

我在可滚动视图中有多个列表视图,因此我使用了一个函数来计算列表视图的高度并将其设置为使列表视图不再可滚动。

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 no,一个用于专业)。

出于测试目的,它们没有任何边距,因此它们应该一个接一个。
如您所见,电子邮件列表视图的高度计算正确,但我的函数对其余列表失败。如果我将“GMC Number”更改为“Email”,那么该函数也会计算 gmc no list view 的正确高度(idk 为什么,这只是一个观察)。

截图 1

我想我的问题与重量有关,但我不知道如何解决它。

先感谢您!

标签: androidandroid-listviewheightscrollable

解决方案


推荐阅读