У меня есть макет, где TextView (переменной ширины) должен иметь возможность перемещаться по горизонтали через все границы родителя (на основе данных и событий представления).
Я попытался добиться этого, используя ConstraintLayout с Руководством. В Руководстве есть ограничение app:layout_constraintGuide_percent
которое программно настраивается между 0 и 1, а TextView ограничен layout_constraintEnd_toEndOf
и layout_constraintStart_toStartOf
для этого Руководства.
Проблема здесь в том, что, когда этот percent
приближается к 0 или 1 (около 0,05 и 0,95 соответственно, в моем случае), TextView начинает исчезать за пределами представления.
В iOS я бы создал ограничение с более высоким приоритетом, ограничивающее TextView родительскими границами, но не представляется ли это возможным при использовании Android ConstraintLayout?
Полный файл макета:
<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:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:paddingTop="20dp"
>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="@+id/guideline"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent"
tools:text="Variable text!"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_constraintTop_toBottomOf="@id/textview"
/>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Вы можете использовать 2 вертикальных направляющих и ограничить ваш textView для них обоих, чтобы, когда 1 направляющая переместилась, изменилась и другая, и ваш textView остался в пределах экрана.
Вот пример (не стесняйтесь перемещать рекомендации к 0,95 и 0,05):
<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:id="@+id/frameLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.MenusDesign.ExpandableCategoriesMenu.ExpandableCategoriesMenu"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:paddingBottom="5dp"
android:visibility="visible"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent"
tools:text="Variable text!" />
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="4dp"
app:layout_constraintTop_toBottomOf="@id/textview"
tools:layout_editor_absoluteX="0dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.0" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.18" />
Другое, что вы можете сделать, это использовать эту библиотеку, чтобы убедиться, что ваш текст изменится в соответствии с оставшимся пространством:
Если вы хотите использовать эту библиотеку, добавьте это в ваш gradle:
implementation 'me.grantland:autofittextview:0.2.+'
А теперь просто создайте свой textView:
<me.grantland.widget.AutofitTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLines="2"
android:textSize="40sp"
autofit:minTextSize="16sp"
/>