如何以编程方式设置ListItems的高度和/或如何以编程方式创建ListItems?我目前使用自定义ListItem.xml与自定义适配器,但ListItems只有一个固定的高度。这会创建大量未使用的空间,因为ListItems的内容不相同。 有没有解决这个问题的方法?Dynamic ListItem高度
回答
您应该使用自定义数组适配器,并且取决于您的物品的内容,您可以更改itemView的高度。你可以在谷歌找到一些例子。
http://sogacity.com/how-to-make-a-custom-arrayadapter-for-listview/
http://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/
在一个ArrayAdapter你可以检查是否有一个字段或没有,和设置其为“视场”高度= 0,例如,或View.INVISIBLE。
我编辑更explicative版本:
在适配器的时候有:getView(int position, View convertView, ViewGroup parent)
,你可以为你的愿望的convertView
分配或多或少的高度修改。如果你不发布一些代码,我不能帮助更多的抱歉。
你的样品还使用了ListItem.xml,但不帮我。就像我说的,我的ListItems的内容是不同的,所以我需要一个解决方案来以编程方式设置高度。 – 2013-02-14 12:28:08
试试这个:
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
而且看到这个链接,这将有助于你 http://kk-brothers.blogspot.in/2011/09/dynamically-change-listview-height.html
- 1. android使listitem高度更小
- 2. 如何制作dijit的高度Accordionpane dynamic
- 3. 如何获取ListView的ListItem高度(使用CustomAdapter)?
- 4. Android - ListItem RelativeLayout文本在TextViews中心的高度wrap_content
- 5. 使水平listview的listitem的高度动态
- 6. 当layout_height更改为wrap_content时,为什么ListItem(LinearLayout)的高度如此之高?
- 7. Centering Dynamic div
- 8. 动态高度列
- 9. 高图泡泡图dynamic-json格式
- 10. UITableViewCell dynamic height:/
- 11. ASP.Net ListItem Value属性最大长度
- 12. 我如何让listitem 100%宽度?
- 13. Dynamic TextView
- 14. Dynamic Bootstrap Well
- 15. 固定高度容器内的两个流体高度div
- 16. Iframe height [dynamic] for each 2 second
- 17. android listitem
- 18. SWFObject 2.0 Dynamic Scaling
- 19. 将高度设置为高度30%的parrent高度的100%?
- 20. Chrome flexbox其他100%高度flexbox溢出100%高度
- 21. 最小高度vs高度
- 22. UIScrollView高度与UIView高度
- 23. %高度的线高度
- 24. Dynamic C++
- 25. Dynamic Multidimensional array
- 26. Angular 2 dynamic html
- 27. 动态的图像宽度和高度
- 28. 内容div剩余高度
- 29. Dynamic Carousel Angular
- 30. UITableViewController Dynamic Drill-Downs
请张贴一些代码... – Anukool 2013-02-14 12:19:33