【问题标题】:Why TextViews running into each other in RelativeLayout为什么TextViews在RelativeLayout中会互相碰撞
【发布时间】:2010-03-17 14:21:02
【问题描述】:

RelativeLayout 中相同高度上的两个 Textview 相互运行时遇到问题。

我使用以下布局。

<?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="fill_parent">

<ImageView 
       android:id="@+id/logo" 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" 
       android:adjustViewBounds="true"
       android:scaleType="centerInside"
       android:src="@drawable/icon" />

<TextView 
       android:id="@+id/name"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" 
       android:text="NameNameNameNameNameNameName"
       android:layout_alignParentTop="true"
       android:layout_toRightOf="@id/logo"
       android:gravity="clip_horizontal" 
       android:lines="1" />

<TextView 
      android:id="@+id/information"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Distance"
      android:layout_alignParentRight="true"
      android:layout_alignParentTop="true" />

<TextView
      android:id="@+id/nrcoupons"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Number"
      android:layout_alignRight="@id/information"
      android:layout_alignBottom="@id/logo" />

<TextView
      android:id="@+id/subcategory"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" 
      android:text="Subcategory"
      android:layout_alignLeft="@id/name"
      android:layout_alignBottom="@id/logo" />

</RelativeLayout>

这给了我这样的观点:

alt text http://janusz.de/~janusz/view.png

一切都是我需要的,除了两个文本视图名称和信息显示在同一个屏幕空间上,一个在另一个之上。

我怎样才能避免这种情况?

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    对于您的@+id/name TextView,添加android:layout_toLeftOf="..." 以获取右侧的TextView。屏幕截图和 XML 似乎没有对齐(屏幕截图似乎在覆盖的 TextView 中有“距离”,但 XML 没有),所以我不完全确定这是哪个小部件。

    如果您的目标是 Android 1.5,则需要在 XML 中订购小部件,以便在从 android:layout_toLeftOfandroid:layout_toRightOf 引用它们之前定义小部件。如果您仅针对 Android 1.6 和更高版本,您可以让它们按任何顺序排列,但任何不同 ID 的第一次出现必须具有 + 符号,即使第一次出现在 android:layout_toLeftOf 属性中而不是android:id 属性。

    【讨论】:

    • 感谢您解决了我的问题。你是对的,我在截图后更改了代码......
    【解决方案2】:

    你的 namenamename textview 设置为 width = fill_parent 所以你不能把任何东西放在它的右边;)

    【讨论】:

    • 将此更改为 wrap_content 不会改变任何内容。正如您在图像中看到的那样,有一些东西放在它的右边并显示出来。可悲的是,它显示在其他视图之外
    猜你喜欢
    • 2019-03-27
    • 2012-10-02
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多