search

Home  >  Q&A  >  body text

android-ui - android ListView的item高度问题?

我写android应用的时候用到listView,但是发现最下面的一个item的一半可显示一半不能显示。

就是问一下不同的android屏幕尺寸不一样,如果我把人listView 的高度设为零,按照weight属性分配高度,然后适配到我手机屏幕显示 5 个 item 刚好能显示全部,是不是总会有些手机的屏幕就是不能显示全部,例如是不是会出现最后一个item遮住一部分的现象?

我想在ListView就显示五个item,不让它们上下滑动,也不希望最后有空白,也不希望最后一个item的一部分被listView的下边框遮住。item好像没有weight属性,高度只能写死,我该怎么实现?

巴扎黑巴扎黑2772 days ago479

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 17:43:50

    If you don’t let ListView slide and the number of items is fixed, why use ListView? Just use LinearLayout...

    Suppose you must use ListView. If the number of items and the height are fixed, then just calculate it yourself and manually set the height of ListView in xml. For example, if you have 5 items and the height of each item is 40dp, then set the height of ListView as 200dp would be fine

    If your item height is not fixed, you can dynamically calculate the height of the ListView based on the item height and quantity in the code, then use LayoutParams设置给ListView, 然后用requestLayout() to redraw the layout, and then put the item into the ListView

    From your description, there is absolutely no need to use ListView. Generally, ListView is only used when all the items to be displayed cannot be displayed on the screen. You are not allowed to slide, so just use other layouts

    reply
    0
  • Cancelreply