Для простоты, скажем, у меня есть два текстовых представления A и B. A - установленный размер с установленным полем между ним и родителем. B является переменным размером. Цель состоит в том, чтобы расположить B по центру ниже A, если он подходит, но не имеет B go за пределами экрана.
Вот xml, который у меня есть в настоящее время для двух элементов:
<?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"
tools:context=".MainActivity">
<TextView
android:id="@+id/element_A"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="elem_A"/>
<TextView
android:id="@+id/element_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@id/element_A"
app:layout_constraintEnd_toEndOf="@id/element_A"
app:layout_constraintTop_toBottomOf="@id/element_A"
android:text="sml"/>
</androidx.constraintlayout.widget.ConstraintLayout>
На рисунке ниже показаны три цифры: слева - то, что я вижу, когда элемент B достаточно мал (большой), в середине - то, что я вижу, когда элемент B слишком большой (не очень), а справа - то, что я вижу. Я хочу видеть, когда элемент B слишком велик.
Как оказалось, переключение с ConstraintLayout на LinearLayout фактически решает эту проблему. Мне все еще интересно, возможно ли это с помощью ConstraintLayout, но сейчас я просто использую LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_gravity="end"
tools:context=".MainActivity">
<TextView
android:id="@+id/element_A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:gravity="end"
android:layout_weight="1"
android:text="elem_A"/>
<TextView
android:id="@+id/element_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
android:text="much much longer element B"/>
</LinearLayout>