What your doing is correct when your using Layout-Weights make sure you set the width as "0dp" rather than wrap-content. So as to get all Heading and list view items to align correctly and give equal weights for each column.
Android how to align headings to listview items
-
29-06-2023 - |
Domanda
I have a Listview in my application, Which I am trying to put a head for each 'Coloumn' however I cant get them to align correctly.
I have made the heading linearlayout items the same layout params as the listView items but they still dont align.
Here is an example app I made to demonstrate the issue.
I have enabled the developer boarder lines so you can easily see how its not aligned.
Here is the list view display item:
<TextView
android:id="@+id/rhinoZoneDisplayItem_textView_zoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:paddingRight="5dp"
android:text="1:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/rhinoZoneDisplayItem_textView_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:clickable="false"
android:gravity="center"
android:text="Status"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
and the headings linear layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Zone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="2"
android:gravity="center_horizontal"
android:text="Status"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Soluzione
Altri suggerimenti
Use match_parent
for TextView
width.
You can put in each item of the list the same value for the field
android:layout_marginLeft="50dp"
this is just an example of the value, in this way all the elements are aligned on left side.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow