2012-06-10 70 views
23

我有一个非常简单的ListView行:的RelativeLayout textviews重叠

<?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="wrap_content" 
android:orientation="horizontal" > 

<TextView 
    android:id="@+id/tournament_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:textSize="25dp" /> 

<TextView 
    android:id="@+id/tournament_winner" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:textSize="25dp" /> 

</RelativeLayout> 

"@+id/tournament_name"文长 - 它从"@+id/tournament_winner"的一个重叠,我不明白为什么。

我试过使用android:singleLine="false"无济于事。我也尝试使用android:inputType="textMultiLine"作为文件说android:singleLine="false"已弃用,但后来我得到警告:Attribute android:inputType should not be used with <TextView>: Change element type to <EditText> ?所以这里也没有好处。

我也试过使用android:ellipsize="end"但这不起作用。我认为这是因为左侧TextView ("@+id/tournament_name")中的文字不足以填满ListView的全部宽度(该代码在此处未播种)。

我确定如果我使用android:layout_width="wrap_content"这两个TextView字段不应该重叠。

下面是一个例子(见第二行):

enter image description here

任何进一步想法如何这可以是固定的?

回答

39
android:layout_toLeftOf="@id/tournament_winner" in First TextView. 

还设置android:maxLines="1"Fix width for tournament winner,因为当它long比赛名称不能看到...

row.xml

<?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="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/tournament_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@id/tournament_winner" 
     android:maxLines="1" 
     android:text="NAMEEEEEEEEEEEEEEEEEEEEEEEEEEE" 
     android:textSize="25dp" /> 

    <TextView 
     android:id="@+id/tournament_winner" 
     android:layout_width="wrap_content" android:layout_marginLeft="10dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:text="WINER" 
     android:textSize="25dp" /> 

</RelativeLayout> 
+1

这不会导致编译时出错吗? tournament_winner现在还不存在,那么如何将任何东西与它对齐呢? – Vas

+0

这是在答案这里解决:https://stackoverflow.com/a/11594737/369480你需要确保你在ID中指定一个+。 – gnuf

+0

ndroid的荣誉:layout_toLeftOf =“@ id/tournament_winner”。它的工作原理 –

11

非常感谢您的回答 - 对不起它了我有一段时间来回应。我结束了使用你的android:layout_toLeftOf="@+id/tournament_winner",但留下了单线和余量未使用,因为结果似乎是完美的(希望这也是其他设备的情况)。

但有一两件事 - 在第一个文本视图(TOURNAMENT_NAME),我不得不使用android:layout_toLeftOf="@+id/tournament_winner"而不是android:layout_toLeftOf="@id/tournament_winner" - 注意添加+。出于某种原因,我得到一个错误,使用android:layout_toLeftOf="@id/tournament_winner"错误:找不到匹配给定名称的资源...所以它似乎有可能和需要在调用它时定义资源,因为系统没有在它被定义之前知道它。

<?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="wrap_content" 
android:orientation="horizontal" > 

<TextView 
    android:id="@+id/tournament_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toLeftOf="@+id/tournament_winner"    
    android:layout_alignParentLeft="true"   
    android:textSize="25dp" /> 

<TextView 
    android:id="@+id/tournament_winner" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"   
    android:layout_alignParentRight="true" 
    android:textSize="25dp" /> 

</RelativeLayout> 
+3

只需更改在xml中添加TextViews的顺序(如果您不想添加+)。所以首先添加tournament_winner然后tournament_name。 – Dante

+0

我不认为你需要使用android:id =“@ + id/tournament_winner”''',在'''layout_toLeftOf'''中已经定义''' –

0

您可以使用单个文本视图来代替两个文本视图,只需在一个文本视图中显示这两个字符串!