CardView内容不尊重match_parent

Fly*_*iew 5 android view android-recyclerview

我正在尝试创建一些非常简单的东西:一个CardView延伸到屏幕高度并且TextView内部应该伸展到卡片宽度的东西.我做了卡填满屏幕垂直android:height="match_parent"CardView.不过,现在的CardView内容,是一个简单的TextView不能在整个卡设置水平时伸展android:height="match_parent"TextView.它只是看起来像wrap_content.

请注意,这CardView是放在RecyclerView一个水平LinearLayoutManager.

请注意,android:layout_width="match_parent"使用水平时卡上的设置实际上并不会将其拉伸到屏幕宽度LinearLayoutManager,如果使用垂直方向则相反LinearLayoutManager!我认为这与我的问题有关.

这是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>
Run Code Online (Sandbox Code Playgroud)

这是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" />
Run Code Online (Sandbox Code Playgroud)

Jig*_*ani 0

这是工作代码。请尝试一下。

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

或者 替换为您的代码。

LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.card_listitem, viewGroup, false);
Run Code Online (Sandbox Code Playgroud)