android - 如何阻止 TextView 超出其他项relativelayout 太远?

标签 android view android-relativelayout

我在相对布局中有一个 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 一直拉伸(stretch)到 ImageView,因为下面还有一些看起来很难看的背景。所以我尝试在两个 View 之间添加一个空白的透明间隔:

<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 提示它是圆形的。

有什么想法吗?

亚历克斯

最佳答案

这是您要找的吗?

将 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>

关于android - 如何阻止 TextView 超出其他项relativelayout 太远?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6484708/

相关文章:

Android SDK 工具 R20,第 3 方库和 Ant 构建的 javadoc

java - 对象输入流读取对象 EOFException

android - 如何隐藏android微调器下拉列表中的第一项?

java - Android:当我还在 View 上设置 layoutParams 时,在 View 上调用 setPadding() 不起作用

c# - 如何使用 EF Code-First 插入/更新可更新 View

android - 在主要相对布局中对齐两个相对布局

android - 如何在 RelativeLayout 中的所有其他 View 后面膨胀/添加 View ?

android - 当手机电量耗尽时,应用程序中是否会调用 onDestroy?

Android 在 View.layout(l,t,r,b) 之后不重绘

android - 将 TextView 垂直于 EditText 居中