Friday 15 June 2012

Android : Align textviews in a listview -



Android : Align textviews in a listview -

i have listvew has 3 textviews. not sure how align them output looks like:

abcded -> xxxx aaa ddd -> qqq bbb alkdladd -> hzhdl ccc

rather getting:

abcded -> xxxx aaa ddd -> qqq bbb alkdladd -> hzhdl ccc

the textviews in first row :

textview1 = abcded textview2 = -> xxxx textview3 = aaa

the problem alignment of textview in middle. how can go doing it?

one alternative have have list item layout contain linearlayout , utilize weights. like.

<linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightsum="6" > <textview android:id="@+id/left_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:singleline="true" android:maxlines="1" android:end="ellipsize"> /> <textview android:id="@+id/center_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" android:singleline="true" android:maxlines="1" android:end="ellipsize" /> <textview android:id="@+id/right_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:singleline="true" android:maxlines="1" android:end="ellipsize" /> </linearlayout>

this makes assumption want text in each view single line. ellipsize (add 3 dots end) of strings otherwise wrap line. can mess weights need to want.

there may other ways using relativelayouts or something, weights (i have found) best way guarantee alignment in apps universally.

android textview alignment

No comments:

Post a Comment