TextView: перенос содержимого в несколько строк - PullRequest
3 голосов
/ 03 мая 2019

У меня есть следующее TextView:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:minLines="2"
    android:text="Lorem ipsum dolor sit amet"/>

Это поведение, которое я получаю:

Это поведениеЯ ожидал:

Я хотел бы получить это поведение без необходимости устанавливать жестко заданную ширину, потому что текст отличается для каждого языка.Есть ли способ сделать это?

РЕДАКТИРОВАТЬ: Я также хотел бы избежать привязки ширины TextView к ширине родителя.Неважно, достаточно ли места для отображения всего текста в одной строке, я хочу, чтобы он отображался в две строки.Учитывая это, я бы хотел, чтобы TextView имел минимально возможную ширину.

1 Ответ

1 голос
/ 03 мая 2019

Да, вы можете использовать ConstraintLayout с Guidelines для определения ширины вашего textView (не жестко запрограммировано), вот простой пример без использования направляющих (тамограничения на текстовое представление):

<?xml version="1.0" encoding="utf-8"?>
  <android.support.constraint.ConstraintLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/mainQuestionsLayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent">


<TextView
    android:id="@+id/textView11"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="TextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdw"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Это будет выглядеть так:

enter image description here

Теперь, если вы хотите иметь более гибкую ширину , вы можете использовать направляющие или просто перенести ограничения вашего текстового представления в другое представление и установить его ширину 0dp

Вотпример с указанием:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/mainQuestionsLayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent">


<TextView
    android:id="@+id/textView11"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="TextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdw"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/guideline19"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.constraint.Guideline
    android:id="@+id/guideline19"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5" />
</android.support.constraint.ConstraintLayout>

И это будет выглядеть так:

enter image description here

Обратите внимание, что пунктирная линия - это реальная направляющая линия, вот как она выглядит в предварительном просмотре проекта, вы не увидите направляющую линию во время выполнения, но ваш textView будет выглядеть следующим образом.

...