Как отобразить список относительных раскладок? - PullRequest
2 голосов
/ 07 апреля 2020

У меня проблема, я веб-разработчик, я привык к CSS, и я так хорош в этом, я думал, что это будет то же самое для Android дизайна, но у меня плохое время на самом деле, я потратил почти половину дня, просто проектируя только верхнее меню и размещая две кнопки под ним, и я не думаю, что сделал это даже правильно, теперь у меня есть блок blo c, где я отображаю изображение, имя пользователя и кнопку на Вот как это выглядит:

     <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="70dp">
                <Button
                    android:id="@+id/button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Ajouter"
                    android:layout_marginRight="40px"
                    android:layout_marginTop="30px"

                    />
                <LinearLayout
                    android:orientation="horizontal"
                    android:id="@+id/bun"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="something"
                    android:layout_alignParentLeft="true"
                    android:layout_margin="10px"
                    >
                    <TextView
                        android:layout_width="350px"
                        android:layout_height="30dp"
                        android:layout_marginTop="45px"
                        android:layout_marginLeft="0px"

                        android:textSize="22sp"
                        tools:text="AkkaBook"></TextView>
                    <ImageView
                        android:layout_width="100px"
                        android:layout_height="100px"
                        android:layout_marginTop="40px"
                        android:layout_marginRight="40px"
                        android:layout_marginLeft="40px"
                        android:id="@+id/profgilImg"
                        android:background="@color/colorPrimary"></ImageView>

                </LinearLayout>


            </RelativeLayout>

        </RelativeLayout> 

Это относительный макет, который должен содержать множество относительных макетов, каждый из этих дочерних относительных макетов имеет содержимое (изображение - имя пользователя - кнопка). Мне удалось сделать только один, но когда я копирую и вставляю код, чтобы получить полный список, все начинает выглядеть ужасно.

Вот как я выглядела:

enter image description here

Мне нужен список:

enter image description here

Любая помощь будет высоко ценится.

Ответы [ 2 ]

2 голосов
/ 07 апреля 2020

Вы должны использовать два разных макета. Потому что я думаю, что у вас есть один основной макет, который включает в себя recyclerView и макет элемента для вашего recyclerView. Ваши макеты должны быть такими:

Основной макет

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/linearLayoutHeader"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="LISTE DES AMIS"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="SUGGESTIONS"/>

    </LinearLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/linearLayoutHeader"
        app:layout_constraintBottom_toBottomOf="parent">

     <!-- put your recyclerView here -->

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Элемент RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#e1e1e2">

    <View
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginStart="8dp"
        android:id="@+id/viewView"
        app:layout_constraintStart_toStartOf="parent"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@id/viewView"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginStart="8dp"
        android:text="Akka Book"/>

    <Button
        android:layout_width="50dp"
        android:layout_height="30dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginEnd="8dp"
        android:text="AJOUTER"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Предпочитайте ConstraintLayout, а не RelativeLayout для лучшей производительности. Вот и все

1 голос
/ 07 апреля 2020

Вы должны использовать RecyclerView или ListView, который имеет адаптер с элементом относительной компоновки.

RecyclerView (я рекомендую вам следовать этому руководству)

ListView

Адаптер

...