как установить разные макеты в карт-вид? - PullRequest
0 голосов
/ 19 мая 2019

Я пытаюсь сделать вид карты, как показано ниже

+----------------------------+
|[icon1]   text 1            |
|  ▼                   text3 |
|[icon2]   text 2            |
+----------------------------+

Как я могу создать такую ​​карту?
Я попытался использовать 2 разных макета внутри cardView, но они перекрываются друг с другом.

1 Ответ

0 голосов
/ 19 мая 2019

Если вы посмотрите на документы CardView, вы увидите, что он расширяет FrameLayout, поэтому, если у вас есть несколько дочерних элементов, они будут перекрываться.То, что вам нужно сделать, это иметь один макет в качестве прямого потомка CardView, скажем, ConstraintLayout, и внутри этого ConstraintLayout вы устанавливаете столько дочерних представлений, сколько хотите.

Итак, этот код ...

<androidx.cardview.widget.CardView
        app:cardCornerRadius="8dp"
        android:layout_margin="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/img1"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_margin="16dp"
                android:src="@mipmap/ic_launcher"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageView
                android:id="@+id/img2"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_margin="16dp"
                android:src="@mipmap/ic_launcher"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@id/img1" />

            <TextView
                android:id="@+id/tv1"
                android:text="Hello"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="@+id/img1"
                app:layout_constraintBottom_toBottomOf="@+id/img1"
                app:layout_constraintLeft_toRightOf="@id/img1"
                android:layout_margin="16dp"/>

            <TextView
                android:id="@+id/tv2"
                android:text="World"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="@+id/img2"
                app:layout_constraintBottom_toBottomOf="@+id/img2"
                app:layout_constraintLeft_toRightOf="@id/img2"
                android:layout_margin="16dp"/>

            <TextView
                android:id="@+id/tv3"
                android:text="Hello World!"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_margin="16dp"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>

Будет производить: enter image description here

...