Есть ли лучший способ сделать этот макет? Может быть, с использованием макета ограничений?

#android #android-layout #android-constraintlayout

#Android #android-layout #android-constraintlayout

Вопрос:

Есть ли более простой (лучший) способ сделать этот макет? Может быть, с ConstraintLayout?

Цифра 3333 всегда должна быть в правом нижнем углу, а 2222 — под цифрой 1111.

Не уверен, есть ли лучший способ использовать один RelativeLayout или FrameLayout?

Я пробовал компоновку ограничений, но Android Studio немного сходит с ума при перемещении элементов.

Макет скриншота

   <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true">

<LinearLayout
    android:id="@ id/LeftLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:layout_margin="10dp"
    android:layout_toStartOf="@ id/textView3"
    android:orientation="vertical">

    <TextView
        android:id="@ id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:text="11111111111111111111111111111111111111111111111111111111" />

    <TextView
        android:id="@ id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:text="222222222"
        android:visibility="visible" />

</LinearLayout>

<TextView
    android:id="@ id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="3333"
    android:layout_alignBottom="@ id/LeftLayout"
    android:layout_alignParentEnd="true" />       
</RelativeLayout>
 

Ответ №1:

Используя ConstraintLayout, вы можете создать текстовое представление, содержащее «111» с шириной, установленной на MATCH_CONSTRAINT (0dp), высотой, установленной на maxLines и WRAP_CONTENT . Затем ограничьте его слева и сверху родительского элемента, а справа ограничьте представление, содержащее «3333». Представление с «222» просто ограничено нижней частью «111» по вертикали и слева от родительского элемента. «333» затем просто ограничивается нижней и правой частью родительского элемента.

вот так

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

    <TextView
        android:id="@ id/textView7"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="1111111111111111111111111111111111111111111111111111111111"
        android:maxLines="2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@ id/textView8" />

    <TextView
        android:id="@ id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2222222222"
        app:layout_constraintTop_toBottomOf="@ id/textView7"
        app:layout_constraintLeft_toLeftOf="parent" />

    <TextView
        android:id="@ id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3333"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>
 

Ответ №2:

Вы можете использовать LinearLayout с весами для подобных сценариев.

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="11111111111111111111111111111111111111111" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="2222222222" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="bottom" 
        android:text="3333" />
</LinearLayout>
 

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

1. Таким образом, всего существует только 3 TextView (а не 4), также 3333 должно быть в правом нижнем углу. Итак, вам нужно добавить android: gravity=»bottom» и android:layout_height=»match_parent» в текстовое представление 3333. Мой вопрос скорее в том, предлагает ли макет ограничений что-то более простое.

2. да, вы правы, это, кажется, рекомендуемый, я узнал об этом из программы Android Nanodegree.