RelativeLayout文本视图重叠

roy*_*sch 24 android android-layout

我有一个相当简单的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>
Run Code Online (Sandbox Code Playgroud)

当文本"@+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字段不应该重叠.

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

在此输入图像描述

如何解决这个问题?

Sam*_*iya 44

    android:layout_toLeftOf="@id/tournament_winner" in First TextView.
Run Code Online (Sandbox Code Playgroud)

也设置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>
Run Code Online (Sandbox Code Playgroud)


roy*_*sch 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>
Run Code Online (Sandbox Code Playgroud)

  • 如果您不想添加+,只需更改在xml中添加TextViews的顺序.首先添加tournament_winner然后添加tournament_name. (3认同)