2011-03-11 39 views
1

我有以下代碼:使用的RelativeLayout的佈局一個ListView

public class Events extends ListActivity { 

//... 

private static String[] FROM = {_ID,TIME,TITLE}; 
private static int[] TO = {R.id.rowid,R.id.time,R.id.title,}; 
private void showEvents(Cursor cursor) {  
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, FROM, TO); 
    setListAdapter(adapter); 
} 
} 

的R.layout.item的定義是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal"> 


    <TextView 
    android:id="@+id/rowid" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
    <TextView 
    android:id="@+id/rowidcolon" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text=": " 
    android:layout_toRightOf="@id/rowid" /> 
    <TextView 
    android:id="@+id/time" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_toRightOf="@id/rowidcolon" /> 
    <TextView 
    android:id="@+id/timecolon" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text=": " 
    android:layout_toRightOf="@id/time" /> 
    <TextView 
    android:id="@+id/title" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:ellipsize="end" 
    android:singleLine="true" 
    android:textStyle="italic" 
    android:layout_toRightOf="@id/timecolon" /> 

</RelativeLayout> 

所以我的問題是,當我運行這個程序,列表中唯一顯示的就是行ID textview。顯示後沒有任何文字視圖。有誰知道如何解決這一問題?謝謝。

回答

1

android:layout_width="match_parent" 
android:layout_height="match_parent" 

告訴行ID的TextView是大小父RelativeLayout的相同。如果你想要所有的TextViews水平佈局,你應該使用LinearLayout作爲父級,並設置android:orientation="horizontal"

+0

謝謝,這正是我正在尋找的解決方案。 – PsychoMantis 2011-03-12 20:30:34

0

你的TextView應該有wrap_content作爲它們的寬度,而不是match_parent。這種佈局在水平LinearLayout中可能更容易。請注意,使用層次結構查看器有時會使這類問題容易被發現。

+0

謝謝你的解決方案。我不知道像「層次結構查看器」存在的東西,這對我來說非常有用:) – PsychoMantis 2011-03-12 20:31:37

1

嘗試使用android:layout_width="wrap_content"爲每個TextViews,雖然我懷疑你還必須做其他格式來獲得每個是正確的寬度取決於如果內容文本長度是可變的。