CardView-RelativeLayout заполнить экран 1/4 с RecylerView - PullRequest
1 голос
/ 04 апреля 2020

Я хочу разделить мой экран на 1/4. Я использую RecylerView, но я не мог понять, как я могу это сделать (отзывчиво). Я не знал, какой макет мне использовать. Мой xml файл:

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <RelativeLayout
        android:id="@+id/myLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/cardview_light_background"
        android:padding="16dp"
        >

        <ImageView
            android:id="@+id/imageIv"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:src="@mipmap/ic_launcher"
            />

        <TextView
            android:id="@+id/titleIv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_toEndOf="@id/imageIv"
            android:layout_toRightOf="@+id/imageIv"
            android:text="Title"
            android:textColor="#000"
            android:textSize="22sp"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/descriptionIv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/titleIv"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_toEndOf="@id/imageIv"
            android:layout_toRightOf="@+id/imageIv"
            android:text="Description"
            android:textColor="#000"
            android:textSize="18sp" />


    </RelativeLayout>
</androidx.cardview.widget.CardView> 

Но в результате получилось 4 элемента, которые не заполняют экран. Я просто хочу, чтобы они заполнили весь экран и поделили его на 1/4.

Активность:

<?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=".HomeActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>

1 Ответ

0 голосов
/ 05 апреля 2020

Насколько я понимаю из того, что вы написали, вы хотите поместить Recyclerview на экран и разделить экран на 4. Добавьте 4 Recyclerview на экран, используя LinerarLayout, и присвойте layout_weight значениям 1 для каждого.

В этом;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#FF34FF" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#FFC107" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#00BCD4" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#3F51B5" />
</LinearLayout>

Если вы хотите объяснить, что это не так, и вы хотите иметь 4 элемента в Recyclerview и разделить экран на 4 равные части экрана Это не очень полезно для здоровья. Потому что Recyclerview естественно содержит ScrollView. Он будет скользить вверх и вниз по экрану автоматически. Если ваш экран будет состоять только из 4 элементов, то вы не можете использовать Recyclerview и использовать LinearLayout напрямую, добавить layout_weight value и назначить 1 каждому.

In that;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">

<RelativeLayout
    android:id="@+id/recyclerView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#FF34FF">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="73dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginStart="42dp"
        android:layout_marginTop="89dp"
        android:layout_toEndOf="@+id/button"
        android:text="TextView" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/recyclerView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#FFC107" >

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="66dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginStart="48dp"
        android:layout_marginTop="79dp"
        android:layout_toEndOf="@+id/button2"
        android:text="TextView" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/recyclerView3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#00BCD4" >

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="75dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginStart="51dp"
        android:layout_marginTop="87dp"
        android:layout_toEndOf="@+id/button3"
        android:text="TextView" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/recyclerView4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#3F51B5" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="36dp"
        app:srcCompat="@drawable/ic_launcher_foreground" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginStart="54dp"
        android:layout_marginTop="80dp"
        android:layout_toEndOf="@+id/imageView"
        android:text="TextView" />
</RelativeLayout>
</LinearLayout>

Вместо RelativeLayout вы можете использовать желаемый макет.

...