Как сделать так, чтобы 4 компонента вписывались в макет ограничения с одинаковым пространством вокруг и между ними - PullRequest
1 голос
/ 14 июля 2020

У меня есть 4 компонента (блока) в ConstraintLayout, но проблема в том, что пространство между этими компонентами больше, чем пространство вокруг них. Я знаю, что проблема в том, что я дал каждому отдельному компоненту запас, но как работать вокруг? для достижения дизайна, указанного ниже

What i want to achieve

What i already achieved

Above is what i have achieved so far.

The main issue bothering is the space between those 4 components, it looks more away from each other than looking compact.

here is my xml code

              app: cardMaxElevation = "5dp" />   android: backgroundTint = "@ color / gradient_end_dashboard" app: cardCornerRadius = "20dp" app: cardElevation = "5dp" app: cardMaxElevation = "5dp" />  

Ответы [ 3 ]

1 голос
/ 14 июля 2020

Попробуйте использовать относительный макет вместо макета фрейма и переместите свой макет / изображение во второй cardView

<RelativeLayout
    android:id="@+id/rl_top_left"
    android:layout_height="0dp"
    android:layout_width="0dp"
    app:layout_constraintBottom_toTopOf="@+id/rl_bottom_left"
    app:layout_constraintEnd_toStartOf="@+id/rl_top_right"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.cardview.widget.CardView
        android:backgroundTint="#163042"
        android:layout_gravity="center"
        android:layout_height="match_parent"
        android:layout_marginBottom="3dp"
        android:layout_marginEnd="7dp"
        android:layout_marginStart="7dp"
        android:layout_marginTop="7dp"
        android:layout_width="match_parent"
        app:cardCornerRadius="20dp"
        app:cardElevation="5dp"
        app:cardMaxElevation="5dp" />

    <androidx.cardview.widget.CardView
        android:backgroundTint="#ffffff"
        android:layout_gravity="center"
        android:layout_height="match_parent"
        android:layout_margin="7dp"
        android:layout_width="match_parent"
        app:cardCornerRadius="20dp"
        app:cardElevation="5dp"
        app:cardMaxElevation="5dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:elevation="10dp"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_width="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_gravity="center"
                android:layout_height="40dp"
                android:layout_width="40dp"
                android:src="@drawable/ic_apple" />

            <TextView
                android:layout_gravity="center"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_width="wrap_content"
                android:text="Some Text"
                android:textColor="#abcdef"
                android:textSize="22sp"
                android:textStyle="bold" />

            <TextView
                android:layout_gravity="center"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_width="wrap_content"
                android:text="sub text"
                android:textColor="#009688"
                android:textSize="14sp"
                android:textStyle="bold" />


        </LinearLayout>

    </androidx.cardview.widget.CardView>

</RelativeLayout>

<RelativeLayout
    android:id="@+id/rl_top_right"
    android:layout_height="0dp"
    android:layout_width="0dp"
    app:layout_constraintBottom_toTopOf="@+id/rl_bottom_right"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/rl_top_left"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.cardview.widget.CardView
        android:backgroundTint="#163042"
        android:layout_gravity="center"
        android:layout_height="match_parent"
        android:layout_marginBottom="3dp"
        android:layout_marginEnd="7dp"
        android:layout_marginStart="7dp"
        android:layout_marginTop="7dp"
        android:layout_width="match_parent"
        app:cardCornerRadius="20dp"
        app:cardElevation="5dp"
        app:cardMaxElevation="5dp" />

    <androidx.cardview.widget.CardView
        android:backgroundTint="#ffffff"
        android:layout_gravity="center"
        android:layout_height="match_parent"
        android:layout_margin="7dp"
        android:layout_width="match_parent"
        app:cardCornerRadius="20dp"
        app:cardElevation="5dp"
        app:cardMaxElevation="5dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:elevation="10dp"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_width="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_gravity="center"
                android:layout_height="40dp"
                android:layout_width="40dp"
                android:src="@drawable/ic_apple" />

            <TextView
                android:layout_gravity="center"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_width="wrap_content"
                android:text="Some Text"
                android:textColor="#abcdef"
                android:textSize="22sp"
                android:textStyle="bold" />

            <TextView
                android:layout_gravity="center"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_width="wrap_content"
                android:text="sub text"
                android:textColor="#009688"
                android:textSize="14sp"
                android:textStyle="bold" />


        </LinearLayout>

    </androidx.cardview.widget.CardView>

</RelativeLayout>

<RelativeLayout
    android:id="@+id/rl_bottom_left"
    android:layout_height="0dp"
    android:layout_width="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/rl_bottom_right"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/rl_top_left">

    <androidx.cardview.widget.CardView
        android:backgroundTint="#163042"
        android:layout_gravity="center"
        android:layout_height="match_parent"
        android:layout_marginBottom="3dp"
        android:layout_marginEnd="7dp"
        android:layout_marginStart="7dp"
        android:layout_marginTop="7dp"
        android:layout_width="match_parent"
        app:cardCornerRadius="20dp"
        app:cardElevation="5dp"
        app:cardMaxElevation="5dp" />

    <androidx.cardview.widget.CardView
        android:backgroundTint="#ffffff"
        android:layout_gravity="center"
        android:layout_height="match_parent"
        android:layout_margin="7dp"
        android:layout_width="match_parent"
        app:cardCornerRadius="20dp"
        app:cardElevation="5dp"
        app:cardMaxElevation="5dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:elevation="10dp"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_width="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_gravity="center"
                android:layout_height="40dp"
                android:layout_width="40dp"
                android:src="@drawable/ic_apple" />

            <TextView
                android:layout_gravity="center"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_width="wrap_content"
                android:text="Some Text"
                android:textColor="#abcdef"
                android:textSize="22sp"
                android:textStyle="bold" />

            <TextView
                android:layout_gravity="center"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_width="wrap_content"
                android:text="sub text"
                android:textColor="#009688"
                android:textSize="14sp"
                android:textStyle="bold" />


        </LinearLayout>

    </androidx.cardview.widget.CardView>

</RelativeLayout>

<RelativeLayout
    android:id="@+id/rl_bottom_right"
    android:layout_height="0dp"
    android:layout_width="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/rl_bottom_left"
    app:layout_constraintTop_toBottomOf="@+id/rl_top_right">

    <androidx.cardview.widget.CardView
        android:backgroundTint="#163042"
        android:layout_gravity="center"
        android:layout_height="match_parent"
        android:layout_marginBottom="3dp"
        android:layout_marginEnd="7dp"
        android:layout_marginStart="7dp"
        android:layout_marginTop="7dp"
        android:layout_width="match_parent"
        app:cardCornerRadius="20dp"
        app:cardElevation="5dp"
        app:cardMaxElevation="5dp" />

    <androidx.cardview.widget.CardView
        android:backgroundTint="#ffffff"
        android:layout_gravity="center"
        android:layout_height="match_parent"
        android:layout_margin="7dp"
        android:layout_width="match_parent"
        app:cardCornerRadius="20dp"
        app:cardElevation="5dp"
        app:cardMaxElevation="5dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:elevation="10dp"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_width="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_gravity="center"
                android:layout_height="40dp"
                android:layout_width="40dp"
                android:src="@drawable/ic_apple" />

            <TextView
                android:layout_gravity="center"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_width="wrap_content"
                android:text="Some Text"
                android:textColor="#abcdef"
                android:textSize="22sp"
                android:textStyle="bold" />

            <TextView
                android:layout_gravity="center"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_width="wrap_content"
                android:text="sub text"
                android:textColor="#009688"
                android:textSize="14sp"
                android:textStyle="bold" />


        </LinearLayout>

    </androidx.cardview.widget.CardView>

</RelativeLayout>

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

0 голосов
/ 14 июля 2020

вот что я нашел ->

Я включил ваши просмотры карт в комбинацию линейных и относительных

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="#242425"
        android:gravity="center"
        android:layout_marginBottom="-19dp"
        android:orientation="vertical" >


        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginStart="17dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="17dp"
            android:layout_marginBottom="18dp"
            android:backgroundTint="@color/colorPrimary"
            app:cardCornerRadius="20dp"
            app:cardElevation="5dp"
            app:cardMaxElevation="5dp" />

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
     android:layout_centerInParent="true"
            android:backgroundTint="#FFFFFF"
            android:layout_marginStart="15dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="15dp"
            android:layout_marginBottom="40dp"
            app:cardCornerRadius="20dp"
            app:cardElevation="5dp"
            app:cardMaxElevation="5dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="#242425"
        android:gravity="center"
        android:layout_marginBottom="-19dp"

        android:orientation="vertical" >


        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginStart="17dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="17dp"
            android:layout_marginBottom="18dp"
            android:backgroundTint="@color/colorPrimary"
            app:cardCornerRadius="20dp"
            app:cardElevation="5dp"
            app:cardMaxElevation="5dp" />

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_centerInParent="true"
            android:backgroundTint="#FFFFFF"
            android:layout_marginStart="15dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="15dp"
            android:layout_marginBottom="40dp"
            app:cardCornerRadius="20dp"
            app:cardElevation="5dp"
            app:cardMaxElevation="5dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </RelativeLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="#242425"
        android:gravity="center"
        android:orientation="vertical" >



        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginStart="17dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="17dp"
            android:layout_marginBottom="18dp"
            android:backgroundTint="@color/colorPrimary"
            app:cardCornerRadius="20dp"
            app:cardElevation="5dp"
            app:cardMaxElevation="5dp" />

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_centerInParent="true"
            android:backgroundTint="#FFFFFF"
            android:layout_marginStart="15dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="15dp"
            android:layout_marginBottom="40dp"
            app:cardCornerRadius="20dp"
            app:cardElevation="5dp"
            app:cardMaxElevation="5dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="#242425"
        android:gravity="center"
        android:orientation="vertical" >

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginStart="17dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="17dp"
            android:layout_marginBottom="18dp"
            android:backgroundTint="@color/colorPrimaryDark"
            app:cardCornerRadius="20dp"
            app:cardElevation="5dp"
            app:cardMaxElevation="5dp" />

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_centerInParent="true"
            android:backgroundTint="#FFFFFF"
            android:layout_marginStart="15dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="15dp"
            android:layout_marginBottom="40dp"
            app:cardCornerRadius="20dp"
            app:cardElevation="5dp"
            app:cardMaxElevation="5dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </RelativeLayout>
</LinearLayout>

ВЫХОД

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

0 голосов
/ 14 июля 2020

Нет необходимости использовать это GuideLine. Все ваши компоненты должны быть связаны друг с другом, чтобы разрешить динамический c размер каждого компонента (так как вы 0dp для ширины и высоты). Расстояние между компонентами, а также между компонентами и краями экрана можно определить с помощью полей:

<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=".MainActivity">

    <FrameLayout
        android:id="@+id/frameLayout_top_left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/full_margin"
        android:layout_marginTop="@dimen/full_margin"
        android:layout_marginEnd="@dimen/half_margin"
        android:layout_marginBottom="@dimen/half_margin"
        android:background="@android:color/black"
        app:layout_constraintBottom_toTopOf="@id/frameLayout_bottom_left"
        app:layout_constraintEnd_toStartOf="@id/frameLayout_top_right"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/frameLayout_top_right"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/half_margin"
        android:layout_marginTop="@dimen/full_margin"
        android:layout_marginEnd="@dimen/full_margin"
        android:layout_marginBottom="@dimen/half_margin"
        android:background="@android:color/black"
        app:layout_constraintBottom_toTopOf="@id/frameLayout_bottom_right"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/frameLayout_top_left"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/frameLayout_bottom_left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/full_margin"
        android:layout_marginTop="@dimen/half_margin"
        android:layout_marginEnd="@dimen/half_margin"
        android:layout_marginBottom="@dimen/full_margin"
        android:background="@android:color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/frameLayout_bottom_right"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/frameLayout_top_left" />

    <FrameLayout
        android:id="@+id/frameLayout_bottom_right"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/half_margin"
        android:layout_marginTop="@dimen/half_margin"
        android:layout_marginEnd="@dimen/full_margin"
        android:layout_marginBottom="@dimen/full_margin"
        android:background="@android:color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/frameLayout_bottom_left"
        app:layout_constraintTop_toBottomOf="@id/frameLayout_top_right" />

</androidx.constraintlayout.widget.ConstraintLayout>

Результат

...