CardView content does not match match_parent

I am trying to create something very simple: a CardViewthat stretches to the height of the screen and has inside it TextViewthat should stretch to the width of the map. I made a map vertically on the screen android:height="match_parent"on CardView. However, now the content CardView, as simple TextView, will not stretch across the entire map horizontally when installed android:height="match_parent"on TextView. Instead, he looks like wrap_content.

Please note that this one CardViewis placed inside RecyclerViewwith horizontal LinearLayoutManager.

Please note that the installation android:layout_width="match_parent"on the map does not actually stretch it to the width of the screen when using horizontal LinearLayoutManager, but rather, if vertical is used LinearLayoutManager! I think this has something to do with my problem.

Here is the CardView xml file:

<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" />

+4
source share
4 answers

I fixed this by placing CardViewinside a RelativeLayoutas the appearance:

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

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

        ...
+2
source

I think you have a problem with yours inflater. I had a similar problem and changed my inflation:

inflater.inflate(R.layout.services_card_list_item, null);

:

inflater.inflate(R.layout.services_card_list_item, parent, false);
+1
source

. .

public class MyRecyclerAdapter extends RecyclerView.Adapter<FeedListRowHolder> {


    private List<FeedItem> feedItemList;

    private Context mContext;

    public MyRecyclerAdapter(Context context, List<FeedItem> feedItemList) {
        this.feedItemList = feedItemList;
        this.mContext = context;
    }

    @Override
    public FeedListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row,viewGroup, false);
        FeedListRowHolder mh = new FeedListRowHolder(v);
        return mh;
    }

    @Override
    public void onBindViewHolder(FeedListRowHolder feedListRowHolder, int i) {
//        feedListRowHolder.title.setText("set data");
    }

    @Override
    public int getItemCount() {
        return 10;
    }
}

.

LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.card_listitem, viewGroup, false);
0

RecylerView. , , , - : recyclerview-v7: 23.1.0, .

0
source

Source: https://habr.com/ru/post/1614531/


All Articles