相对布局中的文本视图重叠问题

29

我有一个相当简单的ListView行:

<?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 还有没有其他的想法可以解决这个问题?
3个回答

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

还要设置 android:maxLines="1" 以及 为比赛冠军固定宽度,因为当比赛名称过长时,无法看到比赛名称...

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
这个问题在这里的答案中得到了解决:https://dev59.com/YWgu5IYBdhLWcg3w8LWv#11594737 您需要确保在ID中指定一个+。 - gnuf
赞 ndroid:layout_toLeftOf="@id/tournament_winner"。它有效了。 - Mina Wissa

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中添加TextView的顺序。因此,首先添加"tournament_winner",然后再添加"tournament_name"。 - Dante
我认为你不需要使用android:id="@+id/tournament_winner",只需使用android:id="@id/tournament_winner",因为tournament_winner已经在layout_toLeftOf中定义了。 - Jiechao Wang

-4

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接