文本视图的奇怪行为ellipsize

时间:2018-04-21 08:53:06

标签: android android-layout

如果它长于某个宽度,我试图在末尾显示带有...(3个点)的textview。我有以下内容:

 <TextView
            android:id="@+id/tvStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="10dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:maxWidth="160dp"
            android:text="Style asd sad adas cvfvd dff dzxczx zc xcc x c"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

根据文本长度,点的位置和显示的数量会发生变化的问题!

例如 如果文字是:“Style asd sad adas”,则显示为

enter image description here

但如果是:“Style asd sad adas cvfvd df”那么显示器就是

enter image description here

如果是:“样式asd sad adas cvfvd dff dzxczx zc xcc x c”那么显示是

enter image description here

您可以看到文本越长显示越少(背景达到最大长度)。为什么是这样?我怎样才能使它总是显示相同的文本,无论它有多长(在某个阈值之后)。

谢谢

1 个答案:

答案 0 :(得分:0)

这是因为你有android:maxLines="1"。为了支持更大的文本maxlines应该增加。如果maxlines设置为1,即使文本很大,textview也无法跳转到下一行

如果删除android:maxWidth="160dp",它会提供更好的结果

摆脱android:maxLines并使用android:singleLine="true",见下文

<TextView
    android:id="@+id/tvStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="10dp"
    android:ellipsize="end"
    android:maxWidth="160dp"
    android:singleLine="true"
    android:text="Style asd sad adas cvfvd dff dzxczx zc xcc x c"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />