1

我知道有很多类似的问题,我也阅读了很多答案,但没有一个是我想要的。我几乎都试过了。以下是我的尝试(我ellipsize=end为每次尝试设置):

  1. singleLine=true(作品)
  2. lines=1(不行)
  3. maxLines=1(不行)
  4. 将 textView 的宽度设置为特定值(不起作用)
  5. ScrollHorizontally=true(不行)

只有第一个有效,但我想要多行文本而不是一行

有没有其他方法可以实现这一点

任何帮助将不胜感激

编辑:

这是我的 xml 布局(尝试 2):

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="@dimen/x150"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:ellipsize="end"
        android:lines="1"
        android:text="hello hello hello hello hello hello hello hello hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/image" />
</androidx.constraintlayout.widget.ConstraintLayout>

在此处输入图像描述

4

5 回答 5

1

ellipsize=true在之前开发的项目中使用过,在那个项目中运行良好,我对比了当前项目的代码和上一个项目的代码。彼此之间没有区别,所以我对当前项目感到困惑,所以我从github中拉出之前的项目并运行它,发现它在我的xml渲染器中不起作用,但它在我的手机上起作用。

最终我发现这是渲染器的问题。我找到了升级渲染器的按钮。现在我使用最新的渲染器,它现在在渲染器中也可以正常工作。

于 2020-07-28T04:46:53.560 回答
0

这对我来说很好用

<TextView
            android:id="@+id/textView8"
            android:layout_width="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            android:ellipsize="end"
            android:lines="3"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:text="TextVie mbdfdsvffmnsvfbnsddfdfvfndvfnbdvfbndsvfnsdvfnsdvfnsdvfnsbfjnbdsvjfvdsjhfvsdjhvsdsdvfjdvfjsdvfjhsdvfjsdvfjdhsvfhjdvfjdvfhjdvfjdsvfjsvfhjsdvfjsdvfjsdvfjsdvfjsdvfnsdfvbnsdvfbnsdvfnsdvfnsdbvfnbsdvnsdvsdvbfsdnbfw"
            android:textSize="16dp" />
于 2020-07-27T16:18:50.870 回答
0

尝试添加maxLines您想要的行数

<TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:ellipsize="end"
            android:maxLines="4"
            android:text="hello\n hello\n hello\n hello hello hello hello hello hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/image" />
于 2020-07-27T17:57:36.527 回答
0

在java文件中设置属性

yourTextView.setEllipsize(TextUtils.TruncateAt.END);
yourTextView.setMaxLines(3);
yourTextView.setText("long text");
于 2020-07-27T18:29:46.413 回答
0

请注意,仅当文本大小超过允许的行数或文本大小宽度时才会显示省略号;否则,将不会显示。

如果您想要的是在文本末尾始终显示省略号,则必须创建一个连接 (text + "...") 的方法。

像这样的东西:

TextView textView = findViewById(R.id.textView);
    String valueText = textView.getText().toString();
    if (!valueText.isEmpty()) valueText = String.format("%s...", valueText);
    textView.setText(valueText);

下面的例子显示了上面提到的省略号,第一个文本超过了文本视图的大小,第二个没有。

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="2"
        android:text="hello hello hello hello hello hdsafsfdfdel sdsdasdsadasdsadsadasdasdsadsaadssadsadsadsadsasadsadsadsadsadsadsaadsalo hello hello hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/image" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:ellipsize="end"
        android:maxLines="2"
        android:text="hello hello hello hello hello hel sfdsafsafsfasfsafsafsahhhhhhhhhhhhhh"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

在此处输入图像描述

于 2020-07-27T19:23:54.147 回答