Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CardView contents not respecting match_parent

I am trying to create something very simple: a CardView that stretches to screen height and has a TextView inside it which should stretch to the card width. I have made the card fill the screen vertically with android:height="match_parent" on the CardView. However, now the CardView contents, like a simple TextView will not stretch across the whole card horizontally when setting android:height="match_parent" on the TextView. It just looks like wrap_content instead.

Note that this CardView is being placed inside a RecyclerView with a horizontal LinearLayoutManager.

Note that setting android:layout_width="match_parent" on the card does not actually stretch it out to the screen width when using a horizontal LinearLayoutManager, and the opposite is true if using a vertical LinearLayoutManager! I think this has something to do with my problem.

Here is the CardView xml:

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp"
card_view:contentPadding="8dp"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true" >

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

    <ImageView
        android:id="@+id/imageView_video_thumbnail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/textView_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:padding="2sp"
        android:background="#80000000"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:textSize="16sp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <Button
            android:id="@+id/textView_preview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#80000000"
            style="?android:attr/buttonBarButtonStyle"
            android:text="Preview"/>

        <Button
            android:id="@+id/textView_set"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#80000000"
            android:gravity="center"
            style="?android:attr/buttonBarButtonStyle"
            android:text="Set"/>

    </LinearLayout>

</RelativeLayout>

Here is the RecyclerView xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:gravity="center"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_wallpapers" tools:context=".WallpapersActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/cardList"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

like image 302
Flyview Avatar asked Sep 26 '22 07:09

Flyview


1 Answers

I fixed this by placing the CardView inside a RelativeLayout as the outer-most layout:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        style="@style/CardLayout">

        ...
like image 109
Gene Bo Avatar answered Sep 29 '22 07:09

Gene Bo