2011-03-11 82 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,虽然我怀疑你还必须做其他格式来获得每个是正确的宽度取决于如果内容文本长度是可变的。