Android-ellipsize ="end"不显示三个点 [英] Android - ellipsize="end" Not Showing Three Dots
问题描述
我正在处理 ConstraintLayout
中包含的 TextView
.
如果文本长度超过maxLength,我希望ellipsize在文本末尾(在TextView中)添加三个点.
I want ellipsize to add three dots at the end of text(in the TextView) if its length exceeds maxLength.
maxLines ="1"和ellipsize ="end"似乎是最好的答案.不幸的是,它不适用于我的情况.三个点根本不显示.
maxLines="1" and ellipsize="end" seemed to the best answer after a thorough research about my question. Unfortunately, it didn't work for my case. The three dots did't show at all.
这是快照:
原始字符串是"Test for long description"(删除了n).应该显示测试长时间描述...".
The original string was "Test for long description"(The n was dropped). It's supposed to show "Test for long descrip...".
这是我的xml:
<TextView
android:id="@+id/txtDescription"
android:maxLength="24"
android:maxLines="1"
android:ellipsize="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="120dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:text="DescriptionView"
android:textSize="18sp"
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.72" />
我觉得XML中有一些覆盖ellipsize的东西,还是我错过了XML文件中的关键内容?
I feel like there's something override ellipsize in the XML, or did I miss something crucial in the XML file?
推荐答案
问题是:
android:maxLength="24"
将其删除,因为您希望将 TextView
椭圆化.
当TextView的宽度不足以显示整个文本时,它将变为椭圆形.
我认为您不了解 android:ellipsize ="end"
这个属性的全部含义.
如果它足够宽,那么您将不会在末尾看到任何圆点,因为它们是不需要的.
如果您设置 android:layout_width ="40dp"
,那么您将看到圆点.
使用 android:layout_width ="wrap_content"
时, TextView
足够宽并且没有椭圆.
remove it since you want the TextView
to be ellipsized.
A TextView gets ellipsized when it is not wide enough to show the whole text.
I think you do not understand what this attribute android:ellipsize="end"
is all about.
If it is wide enough then you will not see any dots at the end because they are not needed.
If you set android:layout_width="40dp"
then you will see the dots.
With android:layout_width="wrap_content"
the TextView
is wide enough and it is not ellipsized.
这篇关于Android-ellipsize ="end"不显示三个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!