Android 根据字符串长度截断TextView

Android 根据字符串长度截断TextView,android,textview,truncate,android-relativelayout,ellipsize,Android,Textview,Truncate,Android Relativelayout,Ellipsize,我正在尝试实现一个布局,其中两个文本视图(即tv_用户名和tv_日期)将并排对齐。如果用户名较短,则用户名和日期应完整显示;如果用户名较长,则用户名应在末尾截断,日期应完整显示。诸如此类: 在任何情况下,两个文本视图之间的最大间距均应为5dp,即日期应始终位于用户名右侧,边距为5dp,且不与父视图中的右侧对齐。我尝试了下面的代码,但它使用更长的用户名将日期文本视图从屏幕中推出:- <RelativeLayout android:layout_width="matc

我正在尝试实现一个布局,其中两个文本视图(即tv_用户名和tv_日期)将并排对齐。如果用户名较短,则用户名和日期应完整显示;如果用户名较长,则用户名应在末尾截断,日期应完整显示。诸如此类:

在任何情况下,两个文本视图之间的最大间距均应为5dp,即日期应始终位于用户名右侧,边距为5dp,且不与父视图中的右侧对齐。我尝试了下面的代码,但它使用更长的用户名将日期文本视图从屏幕中推出:-

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/value_3"
            android:padding="@dimen/value_5"
            android:paddingEnd="@dimen/value_10"
            android:paddingStart="@dimen/value_10"
            android:paddingTop="@dimen/value_5">

                 <android.support.v7.widget.AppCompatTextView
                android:id="@+id/tv_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:maxLines="@integer/value_1"
               android:text="Samantha George Jefferson and some more text"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/size_fourteen"
                android:layout_alignParentStart="true"/>

            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/tv_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"                
                android:layout_marginLeft="@dimen/value_5"
                android:layout_alignParentRight="true"
                android:text="@string/friendly_date"
                android:textColor="@color/colorBlack_a38"
                android:textSize="@dimen/size_twelv"/>

    </RelativeLayout>

您需要做的就是在您的
tv\u用户名中添加以下行
哪些是

android:singleLine="true"
android:maxLines="1"
android:toLeftOf="+@id/tv_date"
android:toStartOf="+@id/tv_date"
android:ellipsize="end|right"

它的工作方式是一旦在末尾绘制了您的
tv\u日期
,将绘制
tv\u用户名
,如果它需要的空间超过可用空间,它将被截断

您可以通过TableLayout实现这一点

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="0">

    <TableRow>
        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/tv_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="@string/lorem"
            android:textColor="@color/colorPrimary"
            android:textSize="14sp" />

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/tv_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:text="1 hour ago"
            android:textColor="#5000"
            android:textSize="12sp" />
    </TableRow>
</TableLayout>

如@m4n3k4s所述,TableLayout可以做您想做的事情,但是如果您想使用ConstraintLayout来做,那么只需将其添加到xml布局中的TextView中即可

android:maxLines="1"
android:maxWidth="160dp"

显然,将maxWidth设置为您想要的值。

我认为具有权重属性的线性布局将解决您的问题。您可以根据您的要求将重量设置为4:2或3:1,也可以设置为3:2?否则,用约束布局试试。@JaiminModi你能给我一个在这种情况下约束布局的例子吗?使用Linearlayout weight,如果有固定的权重,文本视图之间是否仍保持空间?使用Constraint layout,您可以在布局中通过指定至少两个垂直和水平约束来指定这两个视图之间的空间。有关更多详细信息,请访问谷歌开发者控制台。或者您可以通过设置android:maxEms=“10”(ex)@renzvader-maxEms在这种情况下不起作用来限制tv_用户名大小。谢谢您的回答。单线不是不推荐吗?日期应该。。。在父对象中未向右对齐
android:maxLines="1"
android:maxWidth="160dp"