TextView не помешается текст
При длинном тексте некоторые слова выходят за рамки. Как сделать, чтобы перенос был корректный? Длинный текст в taskView = "В комнате не работает gjjjjfghfhgfhfhgfhgfhggfh fhghfgfgf gfhyyfgh fhfffgh fgfhhf fhhf hghg." По словам переносит, но как то некорректно.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="4dp"
android:background="@drawable/customborder">
<TextView
android:id="@+id/rectangleNumber"
android:layout_width="60dp"
android:layout_height="28dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="@drawable/rectangle"
android:gravity="center_vertical|center_horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/dateTimeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/montserrat"
app:layout_constraintStart_toStartOf="@+id/rectangleNumber"
app:layout_constraintTop_toBottomOf="@+id/rectangleNumber" />
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_medium"
app:layout_constraintStart_toStartOf="@+id/dateTimeView"
app:layout_constraintTop_toBottomOf="@+id/dateTimeView" />
<TextView
android:id="@+id/cvalView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_medium"
app:layout_constraintStart_toStartOf="@+id/address"
app:layout_constraintTop_toBottomOf="@+id/address" />
<TextView
android:id="@+id/taskView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:fontFamily="@font/poppins_medium"
app:layout_constraintStart_toStartOf="@+id/cvalView"
app:layout_constraintTop_toBottomOf="@+id/cvalView" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/montserrat"
app:layout_constraintStart_toStartOf="@+id/taskView"
app:layout_constraintTop_toBottomOf="@+id/taskView" />
<Button
android:id="@+id/agreeBtn"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/rectangle"
android:backgroundTint="#00CC4F"
android:fontFamily="@font/montserrat"
android:text="Принять"
android:textColor="#FFFFFF"
app:layout_constraintStart_toStartOf="@+id/description"
app:layout_constraintTop_toBottomOf="@+id/description" />
<Button
android:id="@+id/disagreeBtn"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="8dp"
android:backgroundTint="#F23B51"
android:text="Отклонить"
android:textColor="#FFFFFF"
android:fontFamily="@font/montserrat"
android:background="@drawable/rectangle"
app:layout_constraintLeft_toRightOf="@+id/agreeBtn"
app:layout_constraintTop_toTopOf="@+id/agreeBtn" />
</androidx.constraintlayout.widget.ConstraintLayout>
Ответы (3 шт):
Попробуйте так:
<TextView
android:id="@+id/taskView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="start"
android:fontFamily="@font/poppins_medium"
app:layout_constraintStart_toStartOf="@id/rectangleNumber"
app:layout_constraintEnd_toEndOf="@+id/rectangleNumber"
app:layout_constraintTop_toBottomOf="@+id/cvalView" />
Несколько комментариев к разметке в целом: При использовании ConstraintLayout советую привязывать не к предыдущему элементу, а к тому, от которого действительно есть зависимость. То есть у Вас марджины выставлены для rectangleNumber, остальные элементы тогда привязываем к нему, а не друг к другу - это делает привязку более явной. Иначе теряется понимание того, какие отступы будут. Ну и желательно всегда делать привязку по крайней мере с трёх сторон.
Чтобы TextView внутри ConstraintLayout'a корректно отображал длинные текста, необходимо сперва привязать данный компонент к родительским краям контейнера, либо к ближайшим соседним элементам, и выставить значение ширины в 0dp android:layout_width="0dp.
Пример №1:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Lorem Ipsum является текст-заполнитель обычно используется в графических, печать и издательской индустрии для предварительного просмотра макета и визуальных макетах." />
Есть еще такой вариант. В данном случае мы просто растягиваем ширину TextView на всю длину контейнера android:layout_width="match_parent". Результат идентичный, текст прекрасно переходит на новую строку. Но такой трюк не пройдет если на одной строке вы решите поместить 2 компонента TextView.
Пример №2
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
tools:text="Lorem Ipsum является текст-заполнитель обычно используется в графических, печать и издательской индустрии для предварительного просмотра макета и визуальных макетах." />
Дополнительно могу посоветовать использовать такие атрибуты как ellipsize и maxLines. Что позволит добавить многоточие в конце текста если текст длиннее заданного значения.
Пример №3:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Lorem Ipsum является текст-заполнитель обычно используется в графических, печать и издательской индустрии для предварительного просмотра макета и визуальных макетах." />
Решение вашей проблемы с приложенным скрином:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@color/colorPrimary"
android:padding="8dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/rectangleNumber"
android:layout_width="60dp"
android:layout_height="28dp"
android:layout_margin="4dp"
android:background="#F59723"
android:gravity="center"
app:layout_constraintBottom_toTopOf="@+id/dateTimeView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="72877" />
<TextView
android:id="@+id/dateTimeView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:layout_constraintBottom_toTopOf="@+id/address"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rectangleNumber"
tools:text="2019-12-28 09:00" />
<TextView
android:id="@+id/address"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:layout_constraintBottom_toTopOf="@+id/cvalView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/dateTimeView"
tools:text="Lorem Ipsum" />
<TextView
android:id="@+id/cvalView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:layout_constraintBottom_toTopOf="@+id/taskView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/address"
tools:text="Lorem Ipsum?" />
<TextView
android:id="@+id/taskView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:layout_constraintBottom_toTopOf="@+id/description"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cvalView"
tools:text="Lorem Ipsum! Это текст - рыба, часто используемый в печати и вэб-дизайне. Lorem Ipsum является стандартной рыбой для текстов на латинице с начала XVI века." />
<TextView
android:id="@+id/description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:layout_constraintBottom_toTopOf="@+id/agreeBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/taskView"
tools:text="Это текст - рыба, часто используемый в печати и вэб-дизайне. Lorem Ipsum является стандартной рыбой для текстов на латинице с начала XVI века." />
<Button
android:id="@+id/agreeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:backgroundTint="#00CC4F"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/description"
tools:text="Принять" />
<Button
android:id="@+id/disagreeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#F23B51"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="@+id/agreeBtn"
app:layout_constraintStart_toEndOf="@+id/agreeBtn"
app:layout_constraintTop_toTopOf="@+id/agreeBtn"
tools:text="Отклонить" />
</androidx.constraintlayout.widget.ConstraintLayout>
З.Ы. Предлагаю вам познакомится с компонентом ConstraintLayout чуть ближе в оф доках, плюс можете посмотреть видео по этому компоненту у прекрасной Ребекки Франкс, вот видосик на youtube канале.
З.Ы.Ы. Надеюсь все ясно расписал, будут вопросы, задавайте.
Если в тексте используется /u0020 вместо пробела (Unicode Character SPACE) то данный символ не считается за пробел и перенос происходит по длине строки

