How to Align ListView Text to Right in Android?

后端 未结 6 1477
旧巷少年郎
旧巷少年郎 2021-01-18 15:24

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 th

相关标签:
6条回答
  • 2021-01-18 15:46

    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 enter image description here

    0 讨论(0)
  • 2021-01-18 15:52

    This should suffice

    android:layout_gravity="right" 
    
    0 讨论(0)
  • 2021-01-18 15:54

    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"/>
    
    0 讨论(0)
  • 2021-01-18 15:59

    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.

    0 讨论(0)
  • 2021-01-18 16:02

    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.

    0 讨论(0)
  • 2021-01-18 16:05

    Add the below to the layout

    android:layoutDirection="rtl"
    
    0 讨论(0)
提交回复
热议问题