Android: How to make rows in a ListView have variable heights?
I have a 开发者_如何学GoListView which I want to have a fixed height (say 150dp). Then, inside the ListView, for individual rows, the text can be of varying length and so I would like the rows to be of different heights.
So far, I only got all the rows having same height and so long text got cut-off.
Any way to solve this problem?
Finally, managed to get it to work.
I am using the following adapter code:
ArrayAdapter<String> mcqAnsAdapter = new
ArrayAdapter<String>(this, R.layout.mylist, mcqOptions);
I have created a new layout mylist.xml as follows:
<?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"
/>
The above is similar to the Android defined "simple_list_item_single_choice.xml" except I changed the height to "wrap_content".
The strange thing was that the first time I tried it didn't work. Then, I made some changes here and there and then go back to this version again, it worked. Didn't know what happened. But anyway, very happy to see it working.
Thanks to all who have tried to help. Most appreciated.
Now, another question -- can I put the checkmark on the left of the text instead of the default right side?
You can try to set the row minHeight
to 150dp and the height
to wrap_content
You should use a custom list_item layout, this may help: Android Lists: ListActivity And ListView II – Custom Adapter And List Item View
convertedview.setlayoutparams(new listview.setlayoutparams(width, height));
try like this. It will work.
精彩评论