Andrey Novikov
Andrey Novikov

Reputation: 5593

How do I layout list items to behave like a table rows?

I do not want to use TableLayout because my data model and UI perfectly fit into ListView. But I need to have fields in list items to organize in columns through hole ListView. I want to use two-line list item model with about 4-5 columns in a second line. I do know how to make custom layouts and custom array adapters for ListView. The question is about positioning TextView's in a list item custom layout so that they become nicely aligned. Surely I can not use absolute widths because there are so many screen sizes, but I can not think of any other option.

Upvotes: 1

Views: 602

Answers (1)

user
user

Reputation: 87064

If you wrap your TextViews in a LinearLayout then you can use the layout_weight attribute:

 <LinearLayout android:orientation="horizontal">
    <TextView android:layout_width="0dp" android:layout_weight="25">
    <TextView android:layout_width="0dp" android:layout_weight="25">
    <TextView android:layout_width="0dp" android:layout_weight="25">
    <TextView android:layout_width="0dp" android:layout_weight="25">
  <LinearLayout>

This will make your TextView occupy 25% of the available width and should keep them vertical aligned(you could also set the gravity for the TexView to left). The above code is an example for 4 TextViews, if you have more or less you must adapt the layout_weight attribute so that the sum of all the layout_weight attributes is 100. I hope this is what you want.

Upvotes: 3

Related Questions