Как поддерживать равномерное выравнивание в макете

#android #xml #android-linearlayout #android-percentrelativelayout

#Android #xml #android-linearlayout #android-percentrelativelayout

Вопрос:

Я пытаюсь найти способ поддерживать единообразие текстовых представлений в сложном макете. Есть ли способ расположить «1B» TextView непосредственно под «1A» TextView и над «1C» TextView , избегая при этом слишком большой вложенности?

ПРИМЕЧАНИЕ: «1B» и «1C» TextViews находятся в RelativeLayout вместе как часть расширяемого RecyclerView

введите описание изображения здесь

 <?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    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"
    android:background="@android:color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@ id/linearLayoutA">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@ id/relativeLayoutA">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@ id/ibA"
                android:src="@mipmap/ic_launcher_round"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@ id/tvA"
                android:layout_toEndOf="@ id/ibA"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@ id/relativeLayoutB">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@ id/tvB"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@ id/ivC"
                android:src="@mipmap/ic_launcher_round"
                android:layout_below="@id/tvB"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@ id/tvC"
                android:layout_below="@id/tvB"
                android:layout_toEndOf="@id/ivC"/>
        </RelativeLayout>
    </LinearLayout>

</androidx.cardview.widget.CardView>
  

Ответ №1:

Используйте ConstraintLayout:

 <?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:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@ id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher_round" />

    <TextView
        android:id="@ id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="A 1"
        app:layout_constraintStart_toEndOf="@ id/imageView2"
        app:layout_constraintTop_toTopOf="@ id/imageView2" />

    <TextView
        android:id="@ id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B1"
        app:layout_constraintBottom_toTopOf="@ id/imageView4"
        app:layout_constraintEnd_toEndOf="@ id/textView"
        app:layout_constraintStart_toStartOf="@ id/textView"
        app:layout_constraintTop_toBottomOf="@ id/imageView2" />

    <TextView
        android:id="@ id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="C 1"
        app:layout_constraintEnd_toEndOf="@ id/textView2"
        app:layout_constraintStart_toStartOf="@ id/textView2"
        app:layout_constraintTop_toTopOf="@ id/imageView4" />

    <ImageView
        android:id="@ id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        app:layout_constraintEnd_toEndOf="@ id/imageView2"
        app:layout_constraintStart_toStartOf="@ id/imageView2"
        app:layout_constraintTop_toBottomOf="@ id/imageView2"
        app:srcCompat="@mipmap/ic_launcher_round" />
    </android.support.constraint.ConstraintLayout>
  

Комментарии:

1. textView2 textView3 amp; imageView4 должны быть в каком-то макете для расширения и скрытия в Recyclerview