Android 如何在文本视图末尾显示三个点

Android 如何在文本视图末尾显示三个点,android,android-layout,Android,Android Layout,我在卡片视图下有一个文本视图,当文本足够长时,我想在文本视图的末尾添加三个点 我已尝试在文本视图中添加以下行,但无法帮助我 android:singleLine="true" android:maxLines="1" android:ellipsize="end" 屏幕截图 以下是我的布局: <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_h

我在卡片视图下有一个文本视图,当文本足够长时,我想在文本视图的末尾添加三个点

我已尝试在文本视图中添加以下行,但无法帮助我

android:singleLine="true"
android:maxLines="1"
android:ellipsize="end"  
屏幕截图

以下是我的布局:

 <androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="#fff"
    app:cardCornerRadius="3dp"
    app:contentPadding="3dp"
    app:cardUseCompatPadding="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/homeBookImage"
            android:layout_width="120dp"
            android:layout_height="150dp"
            android:layout_marginEnd="232dp"
            android:layout_marginRight="232dp"
            android:scaleType="fitXY"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

         <TextView
            android:id="@+id/homeBookName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:text=""
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@+id/homeBookImage"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            android:singleLine="true"
            android:maxLines="1"
            android:ellipsize="end"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.cardview.widget.CardView>

有人请让我知道如何才能达到预期的布局。任何帮助都将不胜感激


谢谢

您在
CardView
内部使用了
ConstraintLayout
,我可以看到您已经正确地约束了
TextView
,但是它的宽度是
wrap\u content
,这是导致问题的原因。将其设置为
0dp
,您的
android:ellipsize
android:singleLine
属性将起作用

试试这个:


除此之外,您还需要指定与父项匹配的宽度(以及根据您的设计确定的重量)。
如果文本内容超过长度,则。。。将显示。

提供固定宽度或匹配项

<TextView
       android:layout_width="200dp" // match_parent
       android:layout_height="wrap_content"
       android:singleLine="true"
       android:text="Your text"/>

您可以检查所需的长度,并从代码中执行以下操作:

 if (yourText.length() > 30){ 
      textView = textView .substring(0, 30); //textView length is reduced to add ...
      something.setText(textView + "...");   //adding the ... at the end
 }

尝试调整版面宽度以匹配父项并在设备上运行我的左侧有图像视图,因此当我使其匹配父项时,它与图像视图重叠。请分享问题中的设计我已添加版面截图,请查看。可能重复
 if (yourText.length() > 30){ 
      textView = textView .substring(0, 30); //textView length is reduced to add ...
      something.setText(textView + "...");   //adding the ... at the end
 }