如何配置ListView控件来自动改变其高度? [英] How to configure ListView to automatically change its height?

查看:96
本文介绍了如何配置ListView控件来自动改变其高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个的ListView 部件在同一的LinearLayout 。像这样的东西(我忽略不相关的在这个例子中的XML元素):

I have three ListView widgets in the same LinearLayout. Something like this (I'm omitting XML elements that are not relevant in this example):

<LinearLayout>
    <ListView
      android:layout_width="fill_parent"
      android:layout_height="360dp"/>
    <ListView
      android:layout_width="fill_parent"
      android:layout_height="360dp"/>
    <ListView
      android:layout_width="fill_parent"
      android:layout_height="360dp"/>
</LinearLayout>

这迫使名单有360浸的高度。当然,这将是其高度即使有几个列表项。所以,我的问题是如何使列表有一个自动的高度?我想的是,的ListView 的高度采取它的所有列表项的总和的确切大小。

This forces the list to have a height of 360 dip. Of course, that will be its height even if there are few list items. So, my question is how can make the lists have an automatic height? What I want is that the ListView height takes the exact size of the sum of all its list items.

推荐答案

我已经实现这种方式(code是工作正在进行中,因此更是一个观念源比溶液):

I've implemented it this way (code is work in progress so it's more a idea source than solution):

package com.customcontrols;
public class NoScrollListView extends ListView
{
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, 0) );

        // here I assume that height's being calculated for one-child only, seen it in ListView's source which is actually a bad idea
        int childHeight = getMeasuredHeight() - (getListPaddingTop() + getListPaddingBottom() +  getVerticalFadingEdgeLength() * 2);

        int fullHeight = getListPaddingTop() + getListPaddingBottom() + childHeight*(getCount());

        setMeasuredDimension(getMeasuredWidth(), fullHeight);
    }
}

计算并不完美,但已经很接近了,到目前为止的作品。 之后,您只需创建一个这样的布局:

the calculation's not perfect, but it's close and works so far. after that you just create a layout like this:

滚动型    com.customcontrol.NoScrollListView    com.customcontrol.NoScrollListView    com.customcontrol.NoScrollListView /滚动型

ScrollView com.customcontrol.NoScrollListView com.customcontrol.NoScrollListView com.customcontrol.NoScrollListView /ScrollView

滚动视图的关键,因为你可以轻松地跑出屏幕边界的。

The scrollView's crucial since you can easily run out of screen bounds.

PS。计算的直肠驱动的,因为大多数的ListView控件和放大器的计算方法; Co的私人包这是一个相当奇怪的选择,公开可继承类用户界面。

PS. The calculation's rectum-driven since most of the calculation methods in ListView&Co are package private which is quite a strange choice for publicly inheritable classes for UI.

这篇关于如何配置ListView控件来自动改变其高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆