问题
I have a list view in an Android application used to display an ArrayList containing Strings and I can't find any properties of the ListView that would allow me to align the text of the ListView Items to the right of it instead of the left. Any help would be great, thanks.
回答1:
The answer depends on how exactly do you build the list. If you use the the standard ArrayAdapter then you could use the constructor:
ArrayAdapter<String>(Context, R.layout.aligned_right, values);
where R.layout.aligned_right is a xml layout like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="right"/>
回答2:
This is because these parameters are not defined in the ListView, but in the ListAdapter instead. When you specify an adapter for the list, you should set it to return such a layout which aligns items on the right.
回答3:
Add the below to the layout
android:layoutDirection="rtl"
回答4:
This should suffice
android:layout_gravity="right"
回答5:
For Expandable list view we create two row XMLs namely One is group_row.xml and Other is child_row.xml.
make group_row.xml as
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/row_group_name"
android:layout_width="wrap_content"
android:textSize="24sp"
android:layout_alignParentRight="true"
android:textAllCaps="true"
android:layout_height="wrap_content" />
That is, a Text view aligned to parentRight inside a Relative Layout (which is match parent).
After 2 hours of struggling, I achieved this a moment ago.
image after
回答6:
Pay attention to one small thing.
If you use listitem inside the listview, the gravity attribute of the listitem might not work if the listview layout:width is not set to match_parent.
来源:https://stackoverflow.com/questions/9420114/how-to-align-listview-text-to-right-in-android