Android 相对布局文本视图重叠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10972436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RelativeLayout textviews overlapping
提问by roysch
I have a rather simple ListView row:
我有一个相当简单的 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>
When the text of "@+id/tournament_name"
is long - it overlaps with the one from "@+id/tournament_winner"
and I don't understand why.
当文本"@+id/tournament_name"
很长时 - 它与来自的文本重叠,"@+id/tournament_winner"
我不明白为什么。
I tried using android:singleLine="false"
to no avail. I also tried using android:inputType="textMultiLine"
as the docu says android:singleLine="false"
is deprecated but then I get the warning: Attribute android:inputType should not be used with <TextView>: Change element type to
<EditText> ?
so no good here as well.
我尝试使用android:singleLine="false"
无济于事。我也尝试使用android:inputType="textMultiLine"
docu 所说的android:singleLine="false"
已弃用但后来我收到警告:Attribute android:inputType should not be used with <TextView>: Change element type to
<EditText> ?
所以这里也不好。
I also tried using android:ellipsize="end"
but this doesn't work. I assume it is because the text in the left TextView ("@+id/tournament_name")
is NOT long enough to fill up the full width of the ListView
(which code is not sowing here).
我也尝试使用,android:ellipsize="end"
但这不起作用。我认为这是因为左侧的文本TextView ("@+id/tournament_name")
不够长,无法填满ListView
(此处未播种的代码)的整个宽度。
I was sure that if I use android:layout_width="wrap_content"
the two TextView fields shouldn't overlap.
我确信如果我使用android:layout_width="wrap_content"
两个 TextView 字段不应该重叠。
Here is an example (see the second line):
这是一个例子(见第二行):
Any further ideas how this could be fixed?
任何进一步的想法如何解决这个问题?
回答by Samir Mangroliya
android:layout_toLeftOf="@id/tournament_winner" in First TextView.
Also set android:maxLines="1"
and Fix width for tournament winner
because when it gets long tournament name cant see...
也设置了android:maxLines="1"
,Fix width for tournament winner
因为当它变得很长时,锦标赛名称就看不到了......
row.xml
行.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>
回答by roysch
Thank you very much for your answer - sorry it took me some time to respond. I ended up using your android:layout_toLeftOf="@+id/tournament_winner"
but left the single line and the margin to the left unused, as the result seemed perfect to me (hope this is also the case for other devices).
非常感谢您的回答 - 抱歉,我花了一些时间来回复。我最终使用了您的android:layout_toLeftOf="@+id/tournament_winner"
但未使用的单行和左边距,因为结果对我来说似乎很完美(希望其他设备也是如此)。
One thing though - in the first text view (tournament_name) I had to use android:layout_toLeftOf="@+id/tournament_winner"
and not android:layout_toLeftOf="@id/tournament_winner"
- pay attention to the added +. For some reason I get an error using android:layout_toLeftOf="@id/tournament_winner"
: Error: No resource found that matches the given name...so it seems that it is possible and NEEDED to define the resource in the time of calling it because the system doesn't know it before it was defined.
但是有一件事 - 在第一个文本视图(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>
回答by Ashish Mohan
You can use single text view in place of two and simply display both strings in one text view !!
您可以使用单个文本视图代替两个文本视图,只需在一个文本视图中显示两个字符串!