Android: layout_height not working for CardView

I am working with CardViews inside RecyclerView with GridLayoutManager. The problem I am facing is the height I am specifying for the card view inside xml or in java, it is not getting forced by CardView. It looks like the total height of the CardView's child views becomes the height of the CardView . This behavior takes into account the fact that it is the parent layout settings that are applied in their child views.

What am I missing? Am I doing something wrong?

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardView"
    android:layout_width="@dimen/card_view_width"
    android:layout_height="0dp"
    android:layout_marginBottom="6dp"
    android:elevation="10dp"
    android:orientation="horizontal"
    card_view:cardCornerRadius="2dp"

    >

<ImageView
 android:layout_width="@dimen/card_view_width"
    android:layout_height="10dp"
></ImageView>

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

      

In this case, the height of the map view is 0 dp, but still the layout designer preview and when the app is running on the device, the map size is 10 dp.

Regards

+3


source to share


3 answers


What do you expect from what you told the program?

You will report CardVeiw

the height 0dp

with this: android: layout_height = "0dp"

Then you place ImageView

inside and tell it to have height 10dp

.

So, of course, he CardView

makes himself taller to allow him ImageView

to fit in properly.



If you want the inner elements to match the height CardView

, set layout_height

CardView

whatever you like, for example 20dp

:

android:layout_height="20dp"

      

Then they have internal elements match_parent

:

android:layout_height="match_parent"

      

+1


source


Why are you setting visibility to 0? IF you are trying to hide it, use `visibility = View.GONE '.



Also, don't use elevation

that won't work until L. Insteed, use cardElevation to make it work until L.

0


source


Try setting android: layout_height = "wrap_content" to your CardView.

0


source







All Articles