如何根据左侧宽度限制textView的宽度

how to limit textView's width according to left width

我有一个布局(可以是相对的、线性的或约束的)

TextView 与父级左侧对齐,然后 ImageView(固定宽度)从 textView 右侧开始对齐。

我希望先渲染图像,然后再渲染文本视图。

意思是我希望在渲染图像后根据左侧 space 截断文本视图。

  <RelativeLayout
      android:id="@+id/account_name_layout"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:layout_marginBottom="@dimen/account_menu_account_name_layout_bottom_margin">
    <TextView
        android:id="@+id/account_name"
        style="@style/AccountDataAccountName"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:lines="1"
        android:ellipsize="end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        tools:text="emailisverylongaswellwewantittogettruncated@gmail.longdomain.com"/>

    <ImageView
        android:id="@+id/account_name_chevron"
        android:layout_width="@dimen/account_menu_chevron_size"
        android:minWidth="@dimen/account_menu_chevron_size"
        android:layout_height="@dimen/account_menu_chevron_size"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/account_name"
        android:layout_marginTop="@dimen/account_menu_chevron_top_margin"
        android:layout_marginLeft="@dimen/account_menu_chevron_left_margin"/>
  </RelativeLayout>

我试过几个选项:

1) 告诉文本要留给图像视图

2) 对文本视图施加权重 - 在两个元素之间形成间隙。

3) 将 minWidth 设置为图像视图 - 没有帮助只是使图像视图的比例变小。

知道如何先渲染图像以及如何根据左侧宽度限制 textView 的宽度吗?

您可以在 imageView 上强制设置宽度。这将防止 textview 将其推离 space。如果你说你这样做了,请 post 结果图像,因为那没有任何意义。

你上面的例子没有相互约束,没有强制不覆盖或推开。您需要一些约束,例如 "toTheLeftOf" 或 "Weight" 或 LinearLayout 来强制执行它,因为权重仅适用于 LinearLayout。

最简单的方法是只给 imageView 一个硬编码的 DP 宽度和高度,然后在线性布局中将文本设置为 0 宽度和 1 权重。

如果你愿意,你也可以使用百分比,使用 LinearLayout 然后放一个权重总和,例如 100(代表 100%)。然后给你的图像分配它需要的任何百分比,比如 layout_weight=30 并给 textview 70.

这些选项中的任何一个都适合您。如果您尝试了,但没有成功,那么 post 您尝试过的代码将起作用,除非您正在做一些在当前示例中不可见的愚蠢行为。正如我一直这样做的那样,每次你排成一排时,通常左边的图像是固定的,右边的文本是增长的。