ListView无法获得高度。 ImageView图像高度不确定

时间:2018-04-29 11:27:18

标签: android listview imageview height

Imageview来自网络图片,他的高度按宽度缩放

ImageView的

libuv

的ListView

<?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:background="#fff"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
    android:id="@+id/iv_intro_img"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    android:src="@drawable/product_top"
    android:divider="@null"
    android:adjustViewBounds="true"/>
</LinearLayout>

设置ListView高度

<?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="vertical"
        android:layout_marginTop="10dp"
        android:background="#FFFFFF">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:id="@+id/lv_intro"
            android:divider="@null"></ListView>
    </LinearLayout>

但是这个日志一直是打印高度0,

我再次考虑,添加一个ImageView监听事件,但仍然一无所获 我仍然无法获得ImageView高度

public static void setListViewHeightBasedOnChildren(ListView listView, int basicsHeight,int plusItem) {
    int totalHeight = basicsHeight;
    ListAdapter adapter = listView.getAdapter();

    for (int i = 0; i < adapter.getCount(); i++) {
        View listItem = adapter.getView(i, null, listView);
        listItem.measure(0, 0);
        int itemheight=listItem.getMeasuredHeight();
        Log.e("ItemView"+i, "height" + itemheight);
        totalHeight += itemheight + plusItem;
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
    listView.setLayoutParams(params);
}

但还没有。我希望你能帮我设置ListView高度。

1 个答案:

答案 0 :(得分:0)

试试这个为我工作

列出您的适配器数据

lvMedical是您的ListView

   int totalHeight = 0;
        for (int i = 0; i < list.size(); i++) {
            View listItem = listAdapter.getView(i, null, lvMedical);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = lvMedical.getLayoutParams();
        params.height = totalHeight + (lvMedical.getDividerHeight() * (lvMedical.getCount() - 1));
        lvMedical.setLayoutParams(params);
        lvMedical.requestLayout();