当前位置: 首页 > 知识库问答 >
问题:

CardView layout_width=“match_parent”与父级RecyclerView宽度不匹配

楚修为
2023-03-14

我有一个片段包含一个带有layout_width=“match_parent”的RecyclerView:

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment" />

RecyclerView中的项是CardView,也具有layout_width=“match_parent”:

<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/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    card_view:cardCornerRadius="4dp">

    <TextView
        android:layout_gravity="center"
        android:id="@+id/info_text"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent"
        android:textAppearance="?android:textAppearanceLarge"/>
</android.support.v7.widget.CardView>

我将项目视图膨胀如下:

public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        CardView v = (CardView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_listitem, null, true);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

但当我运行该应用程序时,CardView将呈现为wrap_content,如下所示:

请注意,这是在仿真器上运行的,而不是真正的设备。

是我做错了什么,还是虫子?

共有1个答案

雷飞虎
2023-03-14

膨胀的文档:

从指定的xml资源填充新的视图层次结构。如果出现错误,则引发InflateException。

参数
要加载的XML布局资源的资源ID(例如r.layout.main_page)根视图
要作为生成层次结构的父视图(如果attachToRoot为true),或者只是为返回层次结构的根提供一组LayoutParams值的对象(如果attachToRoot为false。)
attachToRoot是否应将膨胀的层次结构附加到根参数?如果为false,root仅用于为XML中的根视图创建正确的LayoutParams子类。返回膨胀层次结构的根视图。如果提供了root,并且attachToRoot为true,则这是root;否则,它是膨胀的XML文件的根。

这里重要的是不要提供true,但要提供父项:

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

提供视图可以让充气器知道要使用什么layoutparams。提供false参数会告诉它暂时不将其附加到父级。这就是recyclerview将为您提供的功能。

 类似资料:
  • 我正在尝试将CardView宽度与屏幕宽度匹配,但CardView的宽度与屏幕宽不匹配。我一次又一次地检查了我的代码,甚至使所有属性的宽度等于匹配的父级。 Recyclerview xml-- 卡片视图xml - 膨胀代码 这是屏幕截图

  • 我有一个,我希望它填满视图的整个宽度,但是如果它比大,它应该保持这个大小。我尝试使用,但它不能用于。

  • 我试图使(“父面板”)的内容从顶部向底部添加,并且使每个组件的宽度与父面板的宽度匹配,同时使每个组件的高度环绕内容。内容将动态生成。 这是我想要的样子: 这在Android中是相当容易做到的。我调查了Swing的布局经理,但他们似乎都不是一个明显的选择。我试过使用(vertical),除了面板没有填充父面板的宽度(这使它看起来非常糟糕)外,它还可以工作。 (注意使用GridBagLayout、Bo

  • 问题内容: 我正在尝试设置此样式: 以便标题位于与图像宽度相同的阴影框中。请注意,有时可以将其放在表的内部。它也不应超过父元素的宽度(必要时将图像缩小)。 这是我到目前为止的内容: 问题在于,放置as 使其不再影响其他元素的流动,因此它会将其覆盖在其下方的内容上,而不是将其下推。 html标记是自动生成的,我无法对其进行任何编辑,因此我想知道使用 纯css 是否可行。 问题答案: 您可以使用CSS

  • 我使用Bootstrap2.3创建了一个html文件。2 css。html将有四个不同高度的行,例如第一行为10%,第二行为20%,第三行为40%,第四行为40%。在第三行中,我在每个单元格中放置了一个框状面板,html正在呈现,但问题是面板的高度与父级不匹配。假设如果面板主体的内容超过,则面板将从父级中出来,如下所示 现在,如果面板主体的内容较少,则面板高度似乎与父级不匹配,如下图所示 我试图实