我正在尝试创建一个文本视图,以便如果字符数超过 22,则显示省略号“...”会出现。然而,这不适用于 maxLength。
<TextView
android:id="@+id/vh_fragnotifications_postedby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginTop="8dp"
android:text="zz12345678901234567890aaaaaaaa"
app:layout_constraintTop_toBottomOf="@+id/vh_fragnotifications_body"
app:layout_constraintLeft_toLeftOf="@+id/vh_fragnotifications_body"
app:layout_constraintRight_toLeftOf="@+id/vh_fragnotifications_dateposted"
android:maxLines="1"
android:ellipsize="end"
android:maxLength="22"/>
如何在保持最大数字 22 的情况下显示省略号?
这是完整的布局:___________________________________________________
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorWhite">
<ImageView
android:id="@+id/vh_fragnotifications_imagepreview"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignTop="@+id/vh_fragnotifications_body"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0"/>
<TextView
android:id="@+id/vh_fragnotifications_body"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
android:textColor="@color/colorBlackFont"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintLeft_toRightOf="@+id/vh_fragnotifications_imagepreview"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/vh_fragnotifications_imagepreview"/>
<TextView
android:id="@+id/vh_fragnotifications_postedby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginTop="8dp"
android:text="zz12345678901234567890aaa"
app:layout_constraintTop_toBottomOf="@+id/vh_fragnotifications_body"
app:layout_constraintLeft_toLeftOf="@+id/vh_fragnotifications_body"
app:layout_constraintRight_toLeftOf="@+id/vh_fragnotifications_dateposted"
android:ellipsize="end"
android:maxLength="22"
android:maxLines="1"/>
<TextView
android:id="@+id/vh_fragnotifications_dateposted"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_below="@+id/vh_fragnotifications_body"
android:layout_marginTop="8dp"
android:text=" • 18 hours ago"
app:layout_constraintTop_toBottomOf="@+id/vh_fragnotifications_body"
app:layout_constraintLeft_toRightOf="@+id/vh_fragnotifications_postedby"
app:layout_constraintRight_toRightOf="@+id/vh_fragnotifications_body"
android:maxLines="1"
android:ellipsize="end" />
<View
android:id="@+id/vh_fragnotifications_divider"
android:layout_width="0dp"
android:layout_height="0.5dp"
android:background="?android:attr/dividerVertical"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toLeftOf="@+id/vh_fragnotifications_body"
app:layout_constraintRight_toRightOf="@+id/vh_fragnotifications_body"
app:layout_constraintTop_toBottomOf="@+id/vh_fragnotifications_dateposted"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Try this
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="8dp"
android:text="zz12345678901234567890aaaaaaaa"
android:maxLines="1"
android:ellipsize="end"
android:maxEms="11"
/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)