Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android:layout_height not working for CardView

I am working with CardViews inside a RecyclerView with GridLayoutManager. The problem that I am facing is, the height I specify for the card view inside xml or in java, it doesn't get enforced by the CardView. It seems like the cumulative height of the child views of the CardView becomes the height of the CardView. This behavior considering the fact that it's parent's layout parameters that are enforced on its 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 card view is 0dp but still the layout designer preview & when the app runs on the device, the size of the card is 10dp.

Best Regards

like image 753
Umer Farooq Avatar asked Nov 10 '22 21:11

Umer Farooq


1 Answers

What do you expect to happen, with what you told the program?

You tell the CardVeiw to have a height of 0dp with this: android:layout_height="0dp"

Then you place an ImageView inside, and tell it to have a height of 10dp.

So of course the CardView makes itself taller to allow the ImageView to fit properly.

If you want the inner elements to match the height of the CardView, set the layout_height of the CardView to whatever you want, like 20dp:

android:layout_height="20dp"

Then have the inner elements match_parent:

android:layout_height="match_parent"
like image 98
Alex K Avatar answered Nov 14 '22 23:11

Alex K