How to remove spaces from a CardView?

1

Good I am trying to create a Cardview but this one leaves me of the LinearLayout, I pass them the code to see if they can help me I am a beginner and surely I am doing something bad.

LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(
                R.id.wrap_content, R.id.wrap_content);
        layoutparams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
        );
        layoutparams.height = 40;
        layoutparams.width = 40;

        CardView catCard = new CardView(getApplicationContext());
        catCard.setLayoutParams(new CardView.LayoutParams(
                CardView.LayoutParams.MATCH_PARENT, 10));

        catCard.setMinimumHeight(10);
        catCard.setCardBackgroundColor(Color.parseColor("#FFC6D6C3"));
        content.addView(catCard);

This is the code of my activity in xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.omar.versionmejorada.Main2Activity"
android:orientation="vertical">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/id_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <android.support.v7.widget.CardView
                android:id="@+id/card3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="8dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="250dp"
                        android:layout_alignParentTop="true"
                        android:src="@drawable/maxresdefault"
                        android:scaleType="centerCrop"/>

                    <TextView
                        android:id="@+id/txt2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="8dp"
                        android:text="Buenos Aires2"
                        android:textColor="#121111"
                        android:textStyle="bold"
                        android:textSize="22dp" />
                </LinearLayout>
            </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
             android:id="@+id/card2"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_margin="8dp">
             <LinearLayout
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:orientation="vertical">

                 <ImageView
                     android:layout_width="match_parent"
                     android:layout_height="250dp"
                     android:layout_alignParentTop="true"
                     android:src="@drawable/maxresdefault"
                     android:scaleType="centerCrop"/>

                 <TextView
                     android:id="@+id/txt1"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:padding="8dp"
                     android:text="Buenos Aires"
                     android:textColor="#121111"
                     android:textStyle="bold"
                     android:textSize="22dp" />
             </LinearLayout>
         </android.support.v7.widget.CardView>
    </LinearLayout>
</ScrollView>

This is the image of how my Cardview works:

As you can see at the bottom is when I try to create the CardVieW by clicking on a button but it comes out of the LinearLayout I hope you can help me.

    
asked by o.flores 22.11.2016 в 15:41
source

2 answers

2

To remove the padding you can do it by adding a negative measure of contentPadding in dp, but ideally the property setPreventCornerOverlap with false value to disable padding.

  

PreventCornerOverlap : On pre-lollipop platforms, CardView does not cut the limits of the Card for rounded corners. Instead   , adds a padding (padding content) that does not overlap with the   rounded corners. You can disable this behavior   setting this field to false .

You can also add the card_view:cardPreventCornerOverlap="false" property to your Widget:

Add this property to your CardView to remove the padding:

   <android.support.v7.widget.CardView
         android:id="@+id/card2"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_margin="8dp"
         card_view:cardPreventCornerOverlap="false">
    
answered by 22.11.2016 в 19:50
1

By default, the cardView has a padding assigned, to eliminate it you can use it in the xml:

card_view:cardPreventCornerOverlap="false"

or in prgramming time:

catCard.setPreventCornerOverlap(false);

Despite this, the natural behavior is to have padding for the depth effect, therefore, it would be a good idea to use another type of container if its characteristics are not used.

    
answered by 22.11.2016 в 17:03