2009-11-02 50 views
54

我有一个相当复杂的ListView,具有可变列表项高度。在某些情况下,我需要在列表项中显示一个额外的视图,默认情况下隐藏(View.GONE)。通过启用它(View.VISIBLE),列表项的高度增加(或至少应该是)。为什么ListView项不能增长以包装其内容?

问题: 即使我将项目的根布局声明为wrap_content,并将项目中的每个组件都填充为fill_parent,但我隐藏/显示应该更改项目高度的视图在底部会被简单切掉其父(项目布局)的高度增长以充分显示它。

是否有任何有关ListViews和项目布局和项目高度,我可能已经错过了陷阱?

一些更多的意见:

出于测试目的,我现在已经减少了列表项的布局只包含根的LinearLayout和ImageView的。当我将LinearLayout高度设置为例如200dip和ImageView fill_parent,我会期望ImageView增长,直到它达到其父级设置的200dip限制。

但是,图像将只会与其位图资源一样高(如果我已将它设置为wrap_content),并且整个列表项将具有相同的高度(即,如果我已将其设置为wrap_content也是)。

然而,如果我将图像高度设置为例如200dip,那么列表项的高度会增加,项目布局也会增加。

换句话说,列表项布局的layout_height被完全忽略,ImageView上的任何高度值都不是硬编码的像素值。

+2

HI .. Matthias 请给我一个工作示例。我也面临同样的问题,但我不清楚你是如何解决的。问题。 – 2011-02-21 10:36:12

回答

54

我设法解决这个问题,但我不明白为什么

正如我所提到的,我已将列表项布局的layout_height设置为wrap_content(因为fill_parent在这里没有意义,因为ListView无限高)。

但是,我已经将中的所有视图layout_height设置为,其布局为fill_parent。相反,将问题设置为wrap_content时问题消失。

这就提出了另外两个问题:

1)什么是要求fill_parent视图的语义,当父wraps_content?哪个大小的请求优先?

2)如果fill_parent显然不起作用,我会如何使视图填充列表项?

感谢您的输入人。

+0

Thx那个Matthias =)我有exakt同样的问题。我也将layout_height设置为“fill_parent”。我认为这很明显。但是,正确的是,当设置为“wrap_content”时,它解决了问题。 奇怪... – Ted 2010-01-06 03:17:50

+1

谢谢!这是在杀我。 – 2011-05-19 02:24:24

+0

好的,出于某种原因,我有类似的问题,但是当我在列表中只有一个项目时,那么整个列表视图对于第一个显示来说太小了。所以当我增加项目的大小时,视图不会增长。我必须将视图设置为“fill_parent”才能工作。现在,我需要看看发生了什么事情,我有负载的项目。 – Mortimer 2012-02-01 15:42:12

21

你怎么膨胀你的行?

如果你不使用它的权利,请尝试使用LayoutInflater#inflate(layoutId, parent, false)(其中parent提供给getView()newView()AdapterView):

v = getLayoutInflater().inflate(R.layout.list_item, parent, false); 
+0

嗨马克,提供一个根视图inflate(),但将attachToRoot设置为false的语义是什么?如果相关视图未附加到rootView,那么该视图还有哪些其他的工作? – Matthias 2009-11-04 14:35:54

+0

对不起,我忘了提及:我为rootView传递null,为attachToRoot传递false。毕竟,视图是由adapter.getView()返回的,我期望将其添加到视图树中。 – Matthias 2009-11-04 14:45:02

+1

从文档中引用“如果为false,则root仅用于为XML中的根视图创建LayoutParams的正确子类”。否则,Android不知道如何将孩子附着到父母身上,所以我认为它使用了非常通用的LayoutParams。 – CommonsWare 2009-11-04 14:45:51

1

我使用“AbsListView.LayoutParams”在“Adapter.getView()”中手动配置宽度和高度。

14

与布局的问题可以通过滚动型引起的是包装

http://developer.android.com/reference/android/widget/ExpandableListView.html

偶然发现了一些注意事项” ......注意:您不能使用了Android的价值WRAP_CONTENT:layout_height如果父级的大小也没有严格指定(例如,如果父级是ScrollView,则无法指定wrap_content,因为它也可以是任意长度)。但是,如果ExpandableListView父级具有特定的长度,则可以使用wrap_content大小,如100像素“。

我删除了包装ScrollView和线性布局开始正常工作。现在只有了解如何将这些东西包装到ScrollView中。上帝帮助我

但无论如何这是非常奇怪的行为。我认为fill_parent不是真的正确的措辞。当使用heirarchyviewer工具时,我总是看到layout_width和leayout_height的WRAP_CONTENT和MATCH_PARENT值。所以可能fill_parent实际上意味着match_parent,这使我处于认知失调状态。

27

试试这个: http://www.java2s.com/Code/Android/UI/setListViewHeightBasedOnChildren.htm

public class Utils { 

    public static void setListViewHeightBasedOnChildren(ListView listView) { 
     ListAdapter listAdapter = listView.getAdapter(); 
     if (listAdapter == null) { 
      // pre-condition 
      return; 
     } 

     int totalHeight = 0; 
     for (int i = 0; i < listAdapter.getCount(); i++) { 
      View listItem = listAdapter.getView(i, null, listView); 
      listItem.measure(0, 0); 
      totalHeight += listItem.getMeasuredHeight(); 
     } 

     ViewGroup.LayoutParams params = listView.getLayoutParams(); 
     params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); 
     listView.setLayoutParams(params); 
     listView.requestLayout(); 
    }  
} 
+1

欢迎来到Stack Overflow!虽然这可能在理论上回答这个问题,[这将是更可取的](http://meta.stackexchange.com/q/8259)在这里包括答案的重要部分,并提供供参考的链接。 – NullUserException 2011-12-06 23:07:27

+1

你在哪里运行这个方法,以便在重新测量孩子之后? – 2012-09-07 17:46:03

+2

不错..但是你需要这个让'params.height'完成一个'px'的值而不是'dp'的值..'int dpValue = totalHeight +(listView.getDividerHeight()*(listAdapter.getCount() - 1)); // dp值 float density = context.getResources()。getDisplayMetrics()。density; params.height =(int)Math.ceil(dpValue * density); //像素值 – 2013-01-04 16:54:22

1

如果你是在为被调整列表项使用自定义View,那么你可以从PeBek的答案,这样当您构建View

addOnLayoutChangeListener(
      new OnLayoutChangeListener() { 
       @Override 
       public void onLayoutChange(View _, int __, int ___, int ____, int bottom, int _____, int ______, 
              int _______, int old_bottom) { 
        final ListView list_view = (ListView) getParent(); 
        final ViewGroup.LayoutParams params = list_view.getLayoutParams(); 
        params.height += bottom - old_bottom; 
        list_view.setLayoutParams(params); 
        list_view.requestLayout(); 
       } 
      }); 

View获得调整大小时,此回调方法将运行并更新ListView高度以包含高度变化(delta botto米)。其他的值,如果你是view(参照视图),lefttoprightbottomold_leftold_topold_rightold_bottom他们证明是有用的。它适用于展开和折叠视图。

API 11OnLayoutChangeListener所必需的,不知道是否有向后兼容的方式这样做。

+0

注意:addOnLayoutChangeListener()需要api level 11 – larham1 2013-01-10 00:02:20

+0

@ larham1指出,当我写这个时,我必须忽略它。 – 2013-01-10 00:06:24

0

我有同样的问题:我想在我的活动中有一个列表,填充所有的屏幕,当设备是垂直比水平方向。 我解决使用人线性布局有高度有FILL_PARENT问题,在项目设置的高度在列表中0dp而layout_weight是二传手为1

android:layout_weight="1" 
android:layout_height="0dp" 
0

这对我的作品

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    android:padding="@dimen/activity_vertical_margin"> 

    <TextView 
     android:id="@+id/tv_status" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:textSize="17sp" 
     android:text="@string/text_list_devices" /> 

    <ListView 
     android:id="@+id/lv_paired" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/activity_vertical_margin" 
     android:cacheColorHint="#00000000" 
     android:layout_above="@+id/signup_t" 
     android:layout_below="@id/tv_status" 
     /> 

    <Button 

     android:id="@+id/signup_t" 
     style="?android:textAppearanceSmall" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:textColor="@android:color/white" 
     android:layout_alignParentBottom="true" 
     android:text="Print All Records" 
     android:typeface="sans" 
     android:layout_marginLeft="45dp" 
     android:layout_marginRight="45dp" 
     android:textSize="24sp" 
     android:background="@drawable/selector_for_button" 
     android:textStyle="bold" /> 
</RelativeLayout> 
相关问题