CardView does not occupy the full screen width despite having match parent property

1

this time I try to make a CardView can occupy the full width of the screen through its property layout_width="match_parent", but when running the application these CardView are shown as if they were configured as "wrap_content" .

The LAYOUT XML RESOURCE

<?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"
    android:paddingLeft="6.67dp"
    android:paddingTop="6.67dp"
    android:paddingRight="6.67dp"
    android:paddingBottom="6.67dp">

    <android.support.v7.widget.CardView
        android:id="@+id/categoriaCV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foreground="?attr/selectableItemBackground"
        app:cardBackgroundColor="#ededed"
        app:cardCornerRadius="4dp"
        app:cardElevation="2dp"
        app:cardPreventCornerOverlap="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imagenProductoIV"
                android:layout_width="97.33dp"
                android:layout_height="97.33dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:layout_marginRight="0dp"
                android:layout_marginBottom="0dp"
                tools:srcCompat="@tools:sample/avatars[0]" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/fondoNombreProducto"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/nombreProductoTV"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_column="0"
                    android:layout_gravity="start"
                    android:layout_marginLeft="6.67dp"
                    android:layout_marginRight="6.67dp"
                    android:text="Nombre de un producto a 2 lineas"
                    android:textColor="@color/nombreProducto"
                    android:textSize="16dp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/etiquetaPrecioTV"
                        android:layout_width="60dp"
                        android:layout_height="wrap_content"
                        android:layout_column="0"
                        android:layout_gravity="start"
                        android:layout_marginLeft="6.67dp"
                        android:layout_marginTop="5dp"
                        android:layout_marginRight="6.67dp"
                        android:text="@string/EtiquetaPrecio"
                        android:textSize="16dp" />

                    <TextView
                        android:id="@+id/precioTV"
                        android:layout_width="60dp"
                        android:layout_height="wrap_content"
                        android:layout_column="1"
                        android:layout_gravity="end"
                        android:layout_marginLeft="6.67dp"
                        android:layout_marginTop="5dp"
                        android:layout_marginRight="6.67dp"
                        android:text="Valor"
                        android:textAlignment="textEnd"
                        android:textSize="16dp" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/etiquetaStockTV"
                        android:layout_width="60dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="left"
                        android:layout_marginLeft="6.67dp"
                        android:layout_marginTop="5dp"
                        android:layout_marginRight="6.67dp"
                        android:text="@string/EtiquetaStock"
                        android:textSize="16dp" />

                    <TextView
                        android:id="@+id/stockTV"
                        android:layout_width="60dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:layout_marginLeft="6.67dp"
                        android:layout_marginTop="5dp"
                        android:layout_marginRight="6.67dp"
                        android:text="Stock"
                        android:textAlignment="textEnd"
                        android:textSize="16dp" />
                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

    </android.support.v7.widget.CardView>
</LinearLayout>

IN THE APP (Gradle)

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support:percent:28.0.0';
    implementation 'com.google.firebase:firebase-core:16.0.5'
    implementation 'com.google.firebase:firebase-auth:16.0.5'
    implementation 'com.google.firebase:firebase-database:16.0.5'
    implementation 'com.google.firebase:firebase-storage:16.0.5'
    implementation 'com.firebaseui:firebase-ui-storage:4.1.0'

    implementation 'com.github.bumptech.glide:glide:4.8.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

}

Why should the "match_parent" not work?

    
asked by Andrés Jara Avila 29.11.2018 в 04:55
source

1 answer

0

I have just found out that the direct father of CardView is RelativeLayout, so if we use a Relative Layout when we use Card View we will not have the problem that Card View omits the match parent , so that's what I did, change the linear layout to a relative layout.

    
answered by 29.11.2018 в 06:42