【问题标题】:How to set different heights for each row in a ListView?如何为 ListView 中的每一行设置不同的高度?
【发布时间】:2011-11-22 16:17:07
【问题描述】:

我在设置 ListView 中单行的高度时遇到问题。我已经阅读了数百篇论坛和博客文章,但似乎没有什么对我有用。

我有一个带有ArrayAdapter<Order> 的 ListView。根据 Order 对象的属性,使用不同的布局,每行具有不同的高度。但是,最后,所有行都具有相同的高度。我想这是给 ArrayAdapter 的构造函数的布局高度。有人知道如何分别控制每行的高度吗?

感谢您的回答, 菲利普

【问题讨论】:

  • 你只需要改变高度(1)或者你想要不同的布局(2)......对于(1)ovveride getView并在那里改变高度......对于(2)你还需要覆盖getItemViewType, getViewTypeCount 和 getView ...无论如何向我们展示您的覆盖 getView 代码

标签: android listview android-arrayadapter


【解决方案1】:

诀窍在于布局项的视图中,您需要将layout_height 设置为wrap_content,仅此而已。

我用不同高度的项目制作了我的 listView 的屏幕截图。

如果您是初学者,这将为您提供一个起点:

public static final String[] bzz = new String[] { "1", "2", "3", "4", "5" };
ArrayAdapter<String> addap = new ArrayAdapter<String>(this,
                R.layout.list_item, R.id.text_view, bzz);
lv.setAdapter(addap);

【讨论】:

  • 这会导致滚动问题
【解决方案2】:

我通过如下更改代码来解决这个问题

  1. 将 list_item 布局更改为 LinearLayout
  2. list_item布局属性中添加android:layout_height="wrap_content"
  3. list_item 的文本视图中添加android:inputType="textMultiLine"

希望它也对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-27
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 2019-07-01
    • 1970-01-01
    相关资源
    最近更新 更多