Android:如何使ListView中的行具有可变高度?

Android:如何使ListView中的行具有可变高度?,android,listview,Android,Listview,我有一个列表视图,我想有一个固定的高度(比如150dp)。然后,在ListView中,对于单独的行,文本可以具有不同的长度,因此我希望这些行具有不同的高度 到目前为止,我只得到所有高度相同的行,所以长文本被截断 有什么办法解决这个问题吗?您可以尝试将行最小高度设置为150dp,将高度设置为包裹内容 您应该使用自定义列表项布局,这可能有助于: 像这样试试。它会起作用的。最后,我们设法让它起作用 我正在使用以下适配器代码: ArrayAdapter<String> mcqAnsAdapt

我有一个列表视图,我想有一个固定的高度(比如150dp)。然后,在ListView中,对于单独的行,文本可以具有不同的长度,因此我希望这些行具有不同的高度

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


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

您可以尝试将行
最小高度设置为150dp,将
高度设置为
包裹内容

您应该使用自定义列表项布局,这可能有助于:


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

最后,我们设法让它起作用

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

ArrayAdapter<String> mcqAnsAdapter = new
       ArrayAdapter<String>(this, R.layout.mylist, mcqOptions);
ArrayAdapter mcqAnsAdapter=new
ArrayAdapter(此,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”类似,除了I 将高度更改为“包裹内容”

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

感谢所有试图帮忙的人。非常感谢


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

是否可以显示listadapter的一些代码?尝试过,但不起作用。我将这段代码用于阵列适配器--ArrayAdapter mcqAnsAdapter=newArrayAdapter(这是android.R.layout.simple\u list\u item\u single\u choice,mcqOptions);我在想,也许问题在于简单的列表项选择有一个预定义的高度“?android:attr/listPreferredItemHeight”。所以我想为自己制作一个定制的,但有问题。有什么想法吗?回答了我自己关于在左边打勾的问题。只需将mylist.xml中的行复选标记行替换为--android:drawableLeft=“?android:attr/listChoiceIndicatorSingle”--它工作得非常好。:-)
<?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"
/>