相对布局和文本视图的叠加问题

4

这是我的当前ListView项目布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="6dp"
android:paddingRight="6dp"
android:paddingBottom="6dp">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="6dp">

    <TextView
        android:id="@+id/departure_line"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:singleLine="true"
        android:textSize="16sp"
        android:textStyle="bold"
        android:shadowColor="#000000"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="1"
        android:paddingLeft="3dp"
        android:paddingRight="3dp" />
</RelativeLayout>

<TextView
    android:id="@+id/departure_destination"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginLeft="52dp"
    android:background="#777777"
    android:singleLine="true"
    android:ellipsize="end"
    android:textSize="16sp" />
<TextView
    android:id="@+id/departure_time"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentRight="true"
    android:textSize="16sp" />
 </RelativeLayout>

我的问题是目标TextView与时间TextView重叠,正如您在屏幕上看到的那样。如何防止这种情况发生?

http://www.abload.de/image.php?img=screensk6y.png

3个回答

1
android:ellipsize="end"

如果文本太长,这将截断文本。或者你可以执行

android:ellipsize="marquee"

它会像Android市场中的应用程序名称一样滚动。有时候你必须扔掉它。

android:singleLine="true"

为了让它工作,需要在那里,我也建议

android:marqueeRepeatLimit="marquee_forever"

这样可以让它继续移动,以防用户第一次错过了它。

布局技巧TextView省略


1
使用属性: android:layout_below="@id/label"
您可以在一个RelativeLayout中完成此操作。并从RelativeLayout中删除orientation属性,它是无用的。
您可以执行以下操作:
  • 使用LinearLayout,其中您具有方向属性。
  • 使用RelativeLayout并查看layout_below属性

如果我做错了什么,请纠正我。我在我的时间视图中添加了“android:layout_below="@+id/departure_destination"”,但现在时间内容被换行包裹了。这不是我想要的。目的地不应该覆盖时间视图,它们应该在前面结束,并且它们的文本应该被截断并填充点。 - CannyDuck
我理解错了你的意思。所以layout_below属性不能解决你的问题。尝试使用ALIGN_RIGHT属性="departure_time"。 - tobias

1

在您的目标中使用android:layout_toLeftOf="@+id/departure_time",或者您可以将它们都设置在TableLayout中


唯一的方法是使用TableLayout,因为RelativeLayout总是在布局排列中覆盖TextViews。 - CannyDuck
实际上这并不是真的,只要你使用布局定义textView的边缘,它就会几乎到达你想要的任何位置,并且高度/宽度将停止在你告诉它的地方。 - KyleM
谢谢您的回答!同时也要恭喜提问者在问题中包含了“覆盖层”这个词。这使得寻找解决方案变得更加容易。 - Eugene van der Merwe

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