Columns in Android ListView

How to make columns in Android ListView? I have this XML layout list item:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <TextView android:layout_weight=".3" android:layout_width="wrap_content" android:layout_height="fill_parent" android:id="@+id/hour" android:gravity="center" /> <ImageView android:id="@+id/symbol" android:scaleType="fitCenter" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight=".5" /> <TextView android:layout_weight=".8" android:layout_width="wrap_content" android:layout_height="fill_parent" android:id="@+id/temperature" android:gravity="center" /> <TextView android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="fill_parent" android:id="@+id/percipitation" android:gravity="center" /> <TextView android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="fill_parent" android:id="@+id/wind_speed" android:gravity="center" /> <TextView android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="fill_parent" android:id="@+id/wind_direction" android:gravity="center" /> </LinearLayout> 

The problem is when f.ex. wind_direction changes from "4" to "300", then the columns are not aligned.

The problem

Who can do this with a fixed column width and using the entire width regardless of devices?

+6
android listview
source share
5 answers

Consider using TableLayout instead of ListView. It is designed to accommodate rows and columns. See Hello, TableLayout .

To add rows programmatically, you can inflate TableRow and call addView in TableLayout.

+3
source share

You can use layout_weight in conjunction with layout_width of 0, like this, in your xml element layout:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/text1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:id="@+id/text2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2"/> </LinearLayout> 

Essentially, the layout_width of 0dp forces all text views to have no width. Since the spare width in the linear layout is crossed out in accordance with layout_weight after determining the width of the text views, you start with a uniform playing field among the text fields that have all the same widths (0). Without width 0, text fields start with different widths and adding extra space (via layout_weight) still leaves text fields of different widths.

+7
source share

You can do one of them instead of wrap_content in TextViews:

 android:layout_width="40dip" 

a higher density is set independent of the density, or

 android:layout_weight="2" 

which you can use to set the percentage width in each text view.

Also see this post and link: What is the difference between "px", "dp", "dip" and "sp" on Android?

+2
source share

Include both. Try using TableLayout instead of LinearLayout to wrap the list item. I think I saw this work somewhere once. Never tried it myself.

http://www.vbsteven.be/blog/using-the-simpleadapter-with-a-listview-in-android/

+1
source share

If you have a lot of data to display, do not use TableLayout. Performance and memory consumption are terrifying. It may take many seconds for your data to be displayed, and you will get a Choreographer error that your application is processing too much in the user interface thread.

+1
source share

All Articles