CardView 宽度与屏幕宽度不匹配?

CardView width is not matching to width of the screen?

我试图将 CardView 宽度与屏幕宽度匹配,但 CardView 宽度与屏幕宽度不匹配。我一次又一次地检查我的代码,我什至让所有的属性宽度都等于父级。

Recyclerview xml --

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DisplayTodayExercises">

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

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

</LinearLayout>

卡片视图xml --

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_general_dimen"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/exer_name"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:layout_height="40dp"
        android:textStyle="bold"
        android:gravity="center_vertical"
        tools:text="Exercise Name"/>


    <TextView
        android:id="@+id/se_eps"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:textStyle="bold"
        android:layout_marginLeft="@dimen/sets_reps_dimen"
        android:gravity="center_vertical"
        android:layout_height="40dp"
        tools:text="5 x 6"/>

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_marginLeft="@dimen/sets_reps_dimen"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"/>

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

充气码

LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.exercises_card_view_layout, null);
    return new ExerciseContentViewHolder(view);

Here is the screenshot

问题出在你的充气机上。查看 LayoutInflater 的文档,特别是 inflate 方法。

试试这个:

LayoutInflater.from(parent.getContext())
        .inflate(R.layout.exercises_card_view_layout, parent, false);

不提供 null,而是提供父视图。这样充气机就知道要使用什么 LayoutParameters。提供 false 参数告诉 LayoutInflater 不要将它附加到父级。 RecyclerView 将为您处理此问题。

这是一个已知问题。您可以使用例如 RelativeLayout 包装您的 CardView 或从此处尝试解决方案:CardView layout_width="match_parent" does not match parent RecyclerView width