如何获得listView的确切高度

时间:2016-12-10 12:25:18

标签: android listview view

在我的布局中,我有一个scrollView,里面有两个listViews。我使用下面发布的代码来获取listviews的高度,并且该代码正确返回了高度。

但是关于第二个listview,我使用相同的代码,但它返回的值非常大于listview的高度。

请让我知道为什么我的listview高度值非常大,以及如何正确获取listview的确切高度?

case R.id.versicherungsListeActivity2mod_lisVie_meineDocs:      

            heightInPIX = (int) this.calcListViewItemsHeight(listView);

            param = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, heightInPIX );
            param.addRule(RelativeLayout.BELOW, ruleSubject);
            container.setLayoutParams(param);
            listView.requestLayout();
            break;
    ....
    ....
    ....
    private float calcListViewItemsHeight(ListView listView) {
    Log.d(TAG, getApplicationContext().getResources().getResourceEntryName(listView.getId()) + ".getCount() :" + listView.getCount());

    int height = 0;
    for (int i = 0; i < listView.getCount(); i++) {
        View childView = listView.getAdapter().getView(i, null, listView);
        childView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        height+= childView.getMeasuredHeight();
    }

    //dividers height
    height += listView.getDividerHeight() * listView.getCount();

    return height;
}

布局

<LinearLayout
            android:id="@+id/versicherungsListeActivity2mod_linLay_verDetailsList_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/versicherungslistsactivity2mod_linLay_detailsBar_container"
            android:layout_marginStart="15dp"
            android:orientation="vertical">

            <ListView
                android:id="@+id/versicherungsListeActivity2mod_lisVie_versDetails"
                android:layout_width="match_parent"
                android:layout_height="match_parent"></ListView>

        </LinearLayout>

1 个答案:

答案 0 :(得分:0)

尝试此代码:

LayoutParams params = (LayoutParams) mListView.getLayoutParams();
lp.height = 300;
mListView.setLayoutParams(params);