【问题标题】:How to stop TextView expanding too far past other items RelativeLayout?如何停止 TextView 扩展超出其他项目 RelativeLayout 太远?
【发布时间】:2011-06-26 14:55:00
【问题描述】:

我在相对布局中有一个 TextView 和一个 ImageView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:paddingLeft="10dip" android:id="@+id/itemRoot"
    android:clickable="false">

    <TextView android:layout_width="wrap_content" android:text="TextView"
        android:layout_height="wrap_content" android:id="@+id/itemTxt"
        android:layout_alignParentLeft="true" android:layout_centerVertical="true"
        android:singleLine="true"></TextView>
    <ImageView android:id="@+id/delBtn" android:layout_height="wrap_content"
        android:src="@drawable/delete" android:layout_width="wrap_content"
        android:layout_alignParentRight="true" android:paddingRight="10dip"
        android:layout_centerVertical="true"></ImageView>

</RelativeLayout>

但是,当文本过长时,它会与 ImageView 重叠,一直到屏幕末尾,然后是“...”。但我希望它在 ImageView 开始的地方停止。我无法将 TextView 一直延伸到 ImageView,因为我下面还有一些看起来很难看的背景。所以我尝试在两个视图之间添加一个空白的透明间隔:

<TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/spacer"
        android:layout_alignTop="@+id/itemTxt" android:layout_alignBottom="@+id/itemTxt"
        android:layout_toLeftOf="@+id/delBtn" android:layout_toRightOf="@+id/itemTxt"
        android:minWidth="5dip" android:background="@android:color/transparent"></TextView>

但是,我仍然有同样的问题。只要文本不太长,分隔符就可以很好地工作。但是因为 itemTxt 是针对 ParentLeft 定义的,所以它只是将垫片推出了屏幕之外,而且我也无法针对垫片定义它,因为 SDK 抱怨它是圆形的。

有什么想法吗?

亚历克斯

【问题讨论】:

    标签: android view android-relativelayout


    【解决方案1】:

    这是你要找的吗?

    将 textview 保持在 Imageview 之上:

     <TextView
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
        android:text="BLaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah"/>
    

    或者你的意思是彼此相邻?

    使用线性布局:

     <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/itemRoot"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="10dip"
        android:orientation="horizontal"
        android:clickable="false">
        <TextView
            android:id="@+id/itemTxt"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:text="TextView"
            android:layout_height="wrap_content"
            android:singleLine="true"/>
        <ImageView
            android:id="@+id/delBtn"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:paddingRight="10dip" 
            android:src="@drawable/delete"/>
    </LinearLayout>
    

    【讨论】:

    • 啊哈,我的意思是后者。然而,这仍然没有奏效,但这给了我一个想法。无论如何,我必须放置 RelativeLayout 以添加更多视图,并且当我在 RelativeLayout 上设置 android:layout_weight="1" 时效果很好。谢谢。
    • @Alex 好啊。考虑一下,要使 LinearLayout 工作,您可能会删除 singleLine 约束。恭喜它工作
    【解决方案2】:

    TextView 之前声明ImageView,以便其id 已经可用,并将以下属性添加到&lt;TextView&gt; 元素:android:layout_toLeftOf="@id/delBtn"

    【讨论】:

    • 这仍然将实际背景一直扩展到 ImageView。
    【解决方案3】:

    首先,使用LinearLayout 和权重代替RelativeLayout 并不是一个好主意,尤其是在ListView 内部使用这个LinearLayout 的情况下。这样做的原因是,当您使用权重时,Android 会进行两次渲染线性布局。而这一事实可能会破坏您的 ListView 的性能。 而不是使用权重,您必须在布局 xml 中的 TextView 之前声明您的 ImageView,然后将此属性添加到您的 TextView:

    android:layout_toLeftOf="@id/delBtn"
    

    【讨论】:

      【解决方案4】:

      在相对布局中,可以通过使用相应属性固定端点(右、左、上、右)来解决此类问题

      因为我们只需要限制宽度,所以根据左右放置端点就足够了 所以只有两个属性可以工作 android:layout_toRightOfandroid:layout_toLeftOf

      所以如果你想要 textView id=地点 在两个视图(TextView、ImageView 或任何其他视图)之间 id=intensity(左侧)和 id=time(右侧) 以下是代码:

      `<TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/place"
              android:layout_toRightOf="@id/intensity"
              android:layout_toLeftOf="@id/time"
              android:text="hokage's house konoha hidden leaf village naruto universe "
              android:textStyle="bold"
              android:layout_below="@id/proximity"
              />`
      

      上面的代码给了我们: image with both atributes of width and large text

      注意如果只使用android:layout_toLeftOf 属性,那么在较小的文本上,文本可能会对齐到右侧image with only one attribute and small text "hokage's home "

      所以使用这两个属性更好 与小文本一起使用也可以提供完美的结果

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-24
        • 2019-03-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多