2012-02-23 22 views
3

我有一個Android應用程序中的列表視圖用於顯示包含字符串的ArrayList,我找不到ListView的任何屬性,這將允許我將ListView項目的文本對齊到右側而不是左側。任何幫助將是偉大的,謝謝。如何在Android中將ListView文本對齊?

+0

感謝大家的幫助。我已經得到它的工作 – John 2012-02-23 20:05:33

回答

10

答案取決於你如何建立列表。如果使用標準的ArrayAdapter,那麼你可以使用構造函數:

ArrayAdapter<String>(Context, R.layout.aligned_right, values); 

其中R.layout.aligned_right是一個xml佈局是這樣的:

<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

上述工作,但我怎樣才能處理顯示滾動條顯示在左側,因爲它現在顯示在項目上 – spacemonkey 2013-12-20 13:11:13

+1

@spacemonkey您無法移動左側的滾動條。難道你不能只在行佈局的根部設置一些像樣的左填充,這樣滾動條就會有一些空間,並且不會與內容重疊? – Luksprog 2013-12-20 13:17:27

+0

完成。感謝隊友讚賞:) – spacemonkey 2013-12-20 13:35:12

1

這應該足夠了

android:layout_gravity="right" 
+0

感謝您的答覆,但我已經嘗試過了。這不會更改ListView中文本的對齊方式。 – John 2012-02-23 19:53:01

+1

您需要將其放入分配給適配器的佈局ID。你將不得不做一個,而不是指定android.R.layout.simple_list_item_1(可能你有什麼?) – styler1972 2012-02-23 19:57:40

3

這是因爲這些參數沒有在ListView定義,但在ListAdapter代替。當您爲列表指定一個適配器時,應該將其設置爲返回這樣的佈局,該佈局對齊右側的項目。

1

添加下面的佈局

android:layoutDirection="rtl" 
1

對於Expandable列表視圖,我們創建兩行XML,即 一個是group_row.xml,其他是child_row.xml。

化妝group_row.xml作爲

<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" /> 

即,對準parentRight一個相對佈局內(這是匹配親本)文本圖。

經過2個小時的掙扎之後,我剛纔完成了這個任務。

圖像後 enter image description here

-1

注重一件小事。

如果在列表視圖中使用listitem,如果列表視圖layout:width未設置爲match_parent,則listitem的gravity屬性可能不起作用。