After trying a long not able to provide space between 2 card view elements inside a recycler view for both horizontal and vertical

huangapple go评论72阅读模式
英文:

After trying a long not able to provide space between 2 card view elements inside a recycler view for both horizontal and vertical

问题

以下是翻译好的部分:

package com.example.cako;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class FirstActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        String []x={"Aaran", "Aaren", "Aarez", "Aarman", "Aaron", "Aaron-James", "Aarron", "Aaryan", "Abhinav","Ankit"};
        String []image= {"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
                "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
                "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Aloo_Tikki_served_with_chutneys.jpg/180px-Aloo_Tikki_served_with_chutneys.jpg",
                "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Aloo_Mattar.jpg/180px-Aloo_Mattar.jpg",
                "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Aloo_Methi_%28Aaloo_Methi%29.JPG/180px-Aloo_Methi_%28Aaloo_Methi%29.JPG",
                "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Spicy_alloo_with_tadka_mirchi.jpg/180px-Spicy_alloo_with_tadka_mirchi.jpg",
                "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
                "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
                "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Aloo_Tikki_served_with_chutneys.jpg/180px-Aloo_Tikki_served_with_chutneys.jpg",
                "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Aloo_Mattar.jpg/180px-Aloo_Mattar.jpg"
        };

        Toolbar toolbar=findViewById(R.id.toolbar) ;
        setSupportActionBar(toolbar);

        RecyclerView horizontalrecyclerView =(RecyclerView)findViewById(R.id.recyclerViewHorizontal);
        horizontalrecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false));
        horizontalrecyclerView.setAdapter(new RecyclerVview(this,x,image));

        RecyclerView verticalrecyclerView =(RecyclerView)findViewById(R.id.recyclerViewVertical);
        verticalrecyclerView.setLayoutManager(new GridLayoutManager(this,2));
        verticalrecyclerView.setAdapter(new RecyclerVview(this,x,image));
    }
}

class RecyclerVview extends RecyclerView.Adapter<RecyclerVview.Holder>{
    Activity co;
    String[] name;
    String[] image;

    public RecyclerVview(Context applicationContext, String[] x, String[] image) {
        co=(Activity)applicationContext;
        name=x;
        this.image=image;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v=LayoutInflater.from(co).inflate(R.layout.layout_for_horizontal_recyclerview,parent,false);
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final Holder holder, int position) {
        holder.textViewForItemName.setText(name[position]);
        Glide.with(co).load(image[position]).into(holder.image);
        holder.floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(co, "yesWorking", Toast.LENGTH_SHORT).show();
                Log.i("yes","log");
            }
        });
        holder.textViewForPrice.setText("30$");
        holder.textViewForPieceInfo.setText("Single Piece Only.");
    }

    @Override
    public int getItemCount() {
        return name.length;
    }

    public class Holder extends RecyclerView.ViewHolder{
        ImageView image;
        TextView textViewForItemName;
        FloatingActionButton floatingActionButton;
        TextView textViewForPrice;
        TextView textViewForPieceInfo;

        public Holder(@NonNull View itemView) {
            super(itemView);
            image=itemView.findViewById(R.id.imageView);
            textViewForItemName=itemView.findViewById(R.id.textView4);
            floatingActionButton=itemView.findViewById(R.id.floatingActionButton);
            textViewForPieceInfo=itemView.findViewById(R.id.textViewForPieceInfo);
            textViewForPrice=itemView.findViewById(R.id.textViewforPrice);

        }
    }
}
英文:

I want to add some space between 2 cards in both the horizontal recycler view and the vert recycler view but not able to add tried everything but won't getting a sol as u can see in the below image For horizontal both the cards are over each other.

After trying a long not able to provide space between 2 card view elements inside a recycler view for both horizontal and vertical

My XML code in which I had defined recycler view Both hor. and ver. :-

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;@color/AppBackground&quot;&gt;
&lt;com.google.android.material.appbar.AppBarLayout
android:id=&quot;@+id/appBar&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
tools:ignore=&quot;MissingConstraints&quot;&gt;
&lt;androidx.appcompat.widget.Toolbar
android:id=&quot;@+id/toolbar&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;?android:attr/actionBarSize&quot;
android:background=&quot;@color/AppBackground&quot;&gt;
&lt;/androidx.appcompat.widget.Toolbar&gt;
&lt;/com.google.android.material.appbar.AppBarLayout&gt;
&lt;androidx.core.widget.NestedScrollView
android:id=&quot;@+id/nestedsview&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:padding=&quot;10dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/appBar&quot;&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:id=&quot;@+id/constraint&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:padding=&quot;10dp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.0&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;&gt;
&lt;TextView
android:id=&quot;@+id/text&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;10dp&quot;
android:fontFamily=&quot;@font/playfair_display_bold_italic&quot;
android:text=&quot;Looking for a delecious cake?&quot;
android:textColor=&quot;@android:color/black&quot;
android:textSize=&quot;20sp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;LinearLayout
android:id=&quot;@+id/linear&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;10dp&quot;
android:orientation=&quot;horizontal&quot;
app:layout_constraintBottom_toTopOf=&quot;@id/recyclerViewHorizontal&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/text&quot;&gt;
&lt;TextView
android:id=&quot;@+id/textView3&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;10dp&quot;
android:fontFamily=&quot;@font/playfair_display_bold_italic&quot;
android:text=&quot;More&quot;
android:textColor=&quot;@android:color/black&quot;
android:textSize=&quot;20sp&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;Button
android:id=&quot;@+id/button&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;200dp&quot;
android:layout_marginEnd=&quot;20dp&quot;
android:text=&quot;Button&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
tools:layout_editor_absoluteX=&quot;266dp&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/recyclerViewHorizontal&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;10dp&quot;
android:paddingLeft=&quot;5dp&quot;
android:paddingTop=&quot;5dp&quot;
android:paddingRight=&quot;5dp&quot;
android:paddingBottom=&quot;5dp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/textView6&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.526&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/linear&quot;
tools:context=&quot;.FirstActivity&quot;&gt;
&lt;/androidx.recyclerview.widget.RecyclerView&gt;
&lt;TextView
android:id=&quot;@+id/textView6&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;0dp&quot;
android:layout_marginTop=&quot;10dp&quot;
android:fontFamily=&quot;@font/playfair_display_bold_italic&quot;
android:text=&quot;Best Selling&quot;
android:textColor=&quot;@android:color/black&quot;
android:textSize=&quot;20sp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/recyclerViewVertical&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/recyclerViewHorizontal&quot; /&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/recyclerViewVertical&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginHorizontal=&quot;20dp&quot;
android:layout_marginStart=&quot;30dp&quot;
android:layout_marginTop=&quot;10dp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/textView6&quot;
tools:context=&quot;.FirstActivity&quot;
&gt;
&lt;/androidx.recyclerview.widget.RecyclerView&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/androidx.core.widget.NestedScrollView&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

My Xml Code for card :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:padding=&quot;20dp&quot;
&gt;
&lt;androidx.cardview.widget.CardView
android:id=&quot;@+id/cardView&quot;
android:layout_width=&quot;170dp&quot;
android:layout_height=&quot;220dp&quot;
android:padding=&quot;10dp&quot;
android:elevation=&quot;10dp&quot;
app:cardBackgroundColor=&quot;@color/CardColor&quot;
app:cardCornerRadius=&quot;25sp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:id=&quot;@+id/constraint&quot;
android:layout_width=&quot;130dp&quot;
android:layout_height=&quot;180dp&quot;
android:layout_gravity=&quot;center&quot;
android:background=&quot;@color/CardColor&quot;
android:padding=&quot;5dp&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/imageView&quot;
android:layout_width=&quot;90dp&quot;
android:layout_height=&quot;100dp&quot;
android:foregroundGravity=&quot;center_horizontal&quot;
android:scaleType=&quot;fitXY&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView4&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;5dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:fontFamily=&quot;@font/playfair_display_bold_italic&quot;
android:gravity=&quot;center&quot;
android:text=&quot;Great&quot;
android:textColor=&quot;@android:color/black&quot;
android:textSize=&quot;12sp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/imageView&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textViewForPieceInfo&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;5dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:fontFamily=&quot;@font/playfair_display_bold_italic&quot;
android:text=&quot;Ram&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/textView4&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textViewforPrice&quot;
android:text=&quot;Shyam&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;5dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:fontFamily=&quot;@font/playfair_display_bold_italic&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/textViewForPieceInfo&quot; /&gt;
&lt;com.google.android.material.floatingactionbutton.FloatingActionButton
android:id=&quot;@+id/floatingActionButton&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginEnd=&quot;10dp&quot;
android:layout_marginBottom=&quot;2dp&quot;
app:fabSize=&quot;mini&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;1.0&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:srcCompat=&quot;@drawable/ic_baseline_add_24&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;
&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

My recycler view class code:

package com.example.cako;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import android.os.Bundle;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class FirstActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
String []x={&quot;Aaran&quot;, &quot;Aaren&quot;, &quot;Aarez&quot;, &quot;Aarman&quot;, &quot;Aaron&quot;, &quot;Aaron-James&quot;, &quot;Aarron&quot;, &quot;Aaryan&quot;, &quot;Abhinav&quot;,&quot;Ankit&quot;};
String []image= {&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Aloo_Tikki_served_with_chutneys.jpg/180px-Aloo_Tikki_served_with_chutneys.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Aloo_Mattar.jpg/180px-Aloo_Mattar.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Aloo_Methi_%28Aaloo_Methi%29.JPG/180px-Aloo_Methi_%28Aaloo_Methi%29.JPG&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Spicy_alloo_with_tadka_mirchi.jpg/180px-Spicy_alloo_with_tadka_mirchi.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Aloo_Tikki_served_with_chutneys.jpg/180px-Aloo_Tikki_served_with_chutneys.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Aloo_Mattar.jpg/180px-Aloo_Mattar.jpg&quot;
};
Toolbar toolbar=findViewById(R.id.toolbar) ;
setSupportActionBar(toolbar);
RecyclerView horizontalrecyclerView =(RecyclerView)findViewById(R.id.recyclerViewHorizontal);
horizontalrecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false));
horizontalrecyclerView.setAdapter(new RecyclerVview(this,x,image));
RecyclerView verticalrecyclerView =(RecyclerView)findViewById(R.id.recyclerViewVertical);
verticalrecyclerView.setLayoutManager(new GridLayoutManager(this,2));
verticalrecyclerView.setAdapter(new RecyclerVview(this,x,image));
}
}
class RecyclerVview extends RecyclerView.Adapter&lt;RecyclerVview.Holder&gt;{
Activity co;
String[] name;
String[] image;
public RecyclerVview(Context applicationContext, String[] x, String[] image) {
co=(Activity)applicationContext;
name=x;
this.image=image;
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v=LayoutInflater.from(co).inflate(R.layout.layout_for_horizontal_recyclerview,parent,false);
return new Holder(v);
}
@Override
public void onBindViewHolder(@NonNull final Holder holder, int position) {
holder.textViewForItemName.setText(name[position]);
Glide.with(co).load(image[position]).into(holder.image);
holder.floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(co, &quot;yesWorking&quot;, Toast.LENGTH_SHORT).show();
Log.i(&quot;yes&quot;,&quot;log&quot;);
}
});
holder.textViewForPrice.setText(&quot;30$&quot;);
holder.textViewForPieceInfo.setText(&quot;Single Piece Only.&quot;);
}
@Override
public int getItemCount() {
return name.length;
}
public class Holder extends RecyclerView.ViewHolder{
ImageView image;
TextView textViewForItemName;
FloatingActionButton floatingActionButton;
TextView textViewForPrice;
TextView textViewForPieceInfo;
public Holder(@NonNull View itemView) {
super(itemView);
image=itemView.findViewById(R.id.imageView);
textViewForItemName=itemView.findViewById(R.id.textView4);
floatingActionButton=itemView.findViewById(R.id.floatingActionButton);
textViewForPieceInfo=itemView.findViewById(R.id.textViewForPieceInfo);
textViewForPrice=itemView.findViewById(R.id.textViewforPrice);
}
}
}

答案1

得分: 1

CarView布局和ConstraintLayout内部添加边距,并在卡片上使用以下内容:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="wrap_content"
    android:layout_height="wrap_content"
    android:margin="8dp">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="170dp"
        android:layout_height="220dp"
        android:margin="8dp"
        android:elevation="10dp"
        app:cardBackgroundColor="@color/CardColor"
        app:cardCornerRadius="25sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraint"
            android:layout_width="130dp"
            android:layout_height="180dp"
            android:layout_gravity="center"
            android:background="@color/CardColor"
            android:padding="5dp">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="90dp"
                android:layout_height="100dp"
                android:foregroundGravity="center_horizontal"
                android:scaleType="fitXY"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                android:layout_marginTop="5dp"
                android:fontFamily="@font/playfair_display_bold_italic"
                android:gravity="center"
                android:text="Great"
                android:textColor="@android:color/black"
                android:textSize="12sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/imageView" />

            <TextView
                android:id="@+id/textViewForPieceInfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                android:layout_marginTop="5dp"
                android:fontFamily="@font/playfair_display_bold_italic"
                android:text="Ram"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView4" />

            <TextView
                android:id="@+id/textViewforPrice"
                android:text="Shyam"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                android:layout_marginTop="5dp"
                android:fontFamily="@font/playfair_display_bold_italic"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textViewForPieceInfo" />

            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/floatingActionButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="10dp"
                android:layout_marginBottom="2dp"
                app:fabSize="mini"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toStartOf="parent"
                app:srcCompat="@drawable/ic_baseline_add_24" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.cardview.widget.CardView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
英文:

Add margin for the CarView layout and inside the ContraintLayout
and Use this for your card.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:margin=&quot;8dp&quot;
&gt;
&lt;androidx.cardview.widget.CardView
android:id=&quot;@+id/cardView&quot;
android:layout_width=&quot;170dp&quot;
android:layout_height=&quot;220dp&quot;
android:margin=&quot;8dp&quot;
android:elevation=&quot;10dp&quot;
app:cardBackgroundColor=&quot;@color/CardColor&quot;
app:cardCornerRadius=&quot;25sp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:id=&quot;@+id/constraint&quot;
android:layout_width=&quot;130dp&quot;
android:layout_height=&quot;180dp&quot;
android:layout_gravity=&quot;center&quot;
android:background=&quot;@color/CardColor&quot;
android:padding=&quot;5dp&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/imageView&quot;
android:layout_width=&quot;90dp&quot;
android:layout_height=&quot;100dp&quot;
android:foregroundGravity=&quot;center_horizontal&quot;
android:scaleType=&quot;fitXY&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView4&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;5dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:fontFamily=&quot;@font/playfair_display_bold_italic&quot;
android:gravity=&quot;center&quot;
android:text=&quot;Great&quot;
android:textColor=&quot;@android:color/black&quot;
android:textSize=&quot;12sp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/imageView&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textViewForPieceInfo&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;5dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:fontFamily=&quot;@font/playfair_display_bold_italic&quot;
android:text=&quot;Ram&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/textView4&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textViewforPrice&quot;
android:text=&quot;Shyam&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;5dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:fontFamily=&quot;@font/playfair_display_bold_italic&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/textViewForPieceInfo&quot; /&gt;
&lt;com.google.android.material.floatingactionbutton.FloatingActionButton
android:id=&quot;@+id/floatingActionButton&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginEnd=&quot;10dp&quot;
android:layout_marginBottom=&quot;2dp&quot;
app:fabSize=&quot;mini&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;1.0&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:srcCompat=&quot;@drawable/ic_baseline_add_24&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;
&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

huangapple
  • 本文由 发表于 2020年10月13日 06:08:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64325943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定