2013-01-17 42 views
0

只是無法將我的列表視圖中的文本對齊到右/左。不知道該怎麼做。 Android:引力似乎不起作用。無法在列表視圖中對齊文本

,這裏是我的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:background= "#FFCC00" 
tools:context=".MainActivity" > 

<RelativeLayout 
    android:layout_weight="0.5" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip"> 
    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:divider="@null" 
     android:dividerHeight="0dp" 
     tools:listitem="@android:layout/simple_list_item_1" 
     android:background= "#FFCC00" 
     android:textColor="#330033" 
     android:gravity="left"> 

    </ListView> 
</RelativeLayout> 

<RelativeLayout 
    android:layout_weight="9" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip"> 
     <TextView 
     android:id="@+id/bookMark" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:textStyle="bold" 
     android:gravity="center" 
     android:text="Bookmarks" 
     android:textColor="#330033" 
     android:background= "#CCCC99" 
     android:onClick="loadBookmarks"/> 
</RelativeLayout> 

這裏是填充ListView

lv = (ListView) findViewById(R.id.listView1); 
    lv.setAdapter(new ArrayAdapter<String>(this,R.layout.row_listview, getResources().getStringArray(R.array.Stories))); 
+0

我不明白你想做什麼 –

回答

0

你可能想改變裏面R.layout.row_listview TextView的文本。 請分享row_listview.xml中的代碼,以便我們幫助您。

+0

不知何故,我無法在eclipse package explorer中看到row_listview.xml。但是我可以在我的Windows資源管理器中的res/layout中查看我的文件夾結構下的xml。我可以看到textview中的引力值存在「中心」。我想那是罪魁禍首。不過,爲什麼row_listview不能在eclipse package explorer中顯示,我感到困惑。 – user1938357

+0

我可以通過糾正row_listview.xml中的重力來解決它。我在錯誤的地方尋找那個xml。感謝康拉德指出了這一點。 – user1938357

0

我認爲,這是你在找什麼的代碼。

android:textAlignment="center"

而且使用android:gravity如果總文本比視圖小的纔有效。所以如果你有「包裝內容」,那麼我不認爲它會起作用。 ATLEAST這就是它說的網站上TextView

下面我添加了一個樣本的佈局和相關的代碼爲校準的是兩個textviews到行

private ListView lv = null; 
@Override 
public View onCreateView(LayoutInflater inflate, ViewGroup container, 
     Bundle savedInstanceState) { 
    View result = inflate.inflate(R.layout.lists_layout, container, false); 
    lv = (ListView) findViewById(R.id.listView1); 

    adapter = new SimpleAdapter(context, things, R.layout.rowLayout, from, to); 
    lv.setAdapter(adapter); 
} 

listLayout.xml的左,右兩側的行

<?xml version="1.0" encoding="utf-8"?> 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    > 

</ListView> 

rowLayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingRight="5dp" > 


    <TextView 
     android:id="@+id/textleft" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft = "true" 
     /> 

    <TextView 
     android:id="@+id/textright" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight = "true" /> 
</LinearLayout> 

+0

我想把我的文字對齊到左邊。但使用android:textAlignment =「left」會拋出xml錯誤,指出「字符串類型不允許」。另外我沒有「wrap_content」。我有寬度和高度fill_parent。 – user1938357

+0

您使用的是Eclipse嗎? – doubleA

+0

無論哪種方式。如果你使用的是eclipse,你可能會缺少一個關鍵特性。突出顯示引號內的文本並按下ctrl空格。這將爲您提供該字段的可能值。您也可以在我發佈在我的答案中的TextView鏈接中查看這些值。 – doubleA