【问题标题】:Android: How to make rows in a ListView have variable heights?Android:如何使 ListView 中的行具有可变高度?
【发布时间】:2011-12-19 21:35:00
【问题描述】:

我有一个 ListView,我希望它有一个固定的高度(比如 150dp)。然后,在 ListView 内部,对于各个行,文本可以具有不同的长度,因此我希望行具有不同的高度。

到目前为止,我只得到了所有具有相同高度的行,所以长文本被截断了。

有什么办法可以解决这个问题?

【问题讨论】:

  • 你能显示一些列表适配器的代码吗?

标签: android listview


【解决方案1】:

终于成功了。

我正在使用以下适配器代码:

ArrayAdapter<String> mcqAnsAdapter = new
       ArrayAdapter<String>(this, R.layout.mylist, mcqOptions);

我创建了一个新的布局 mylist.xml 如下:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/text1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:gravity="center_vertical"
  android:checkMark="?android:attr/listChoiceIndicatorSingle"
  android:paddingLeft="6dip"
  android:paddingRight="6dip"
/>

上面类似于Android定义的“simple_list_item_single_choice.xml”除了我 将高度更改为“wrap_content”。

奇怪的是,我第一次尝试它并没有奏效。然后,我在这里和那里进行了一些更改,然后再次回到这个版本,它起作用了。不知道发生了什么。但无论如何,很高兴看到它工作。

感谢所有试图提供帮助的人。非常感谢。

现在,另一个问题——我可以将复选标记放在文本的左侧而不是默认的右侧吗?

【讨论】:

  • 回答了我自己关于将复选标记放在左侧的问题。只需将 mylist.xml 中的 checkMark 行替换为 -- android:drawableLeft="?android:attr/listChoiceIndicatorSingle" -- 效果非常好。 :-)
【解决方案2】:

您可以尝试将行 minHeight 设置为 150dp,将 height 设置为 wrap_content

您应该使用自定义 list_item 布局,这可能会有所帮助:Android Lists: ListActivity And ListView II – Custom Adapter And List Item View

【讨论】:

  • 试过但不起作用。我正在为数组适配器使用这段代码——ArrayAdapter mcqAnsAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_single_choice, mcqOptions);我在想问题可能出在 simple_list_item_single_choice 上,它有一个预定义的高度“?android:attr/listPreferredItemHeight”。因此,我想为自己创建一个自定义的,但遇到了问题。有什么想法吗?
【解决方案3】:
convertedview.setlayoutparams(new listview.setlayoutparams(width, height));

试试这样。它会起作用的。

【讨论】:

    猜你喜欢
    • 2010-10-12
    • 2015-05-04
    • 2019-07-18
    • 2015-11-18
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多