首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >android全宽度卡在回收视图中的应用

android全宽度卡在回收视图中的应用
EN

Stack Overflow用户
提问于 2016-11-13 17:03:01
回答 6查看 3.2K关注 0票数 3

我使用的是recylcer视图,每一行都包含了硬视图,我需要显示硬视图的全宽度,甚至我的文本视图内容也比较少,.I尝试了很多方法,但是没有用。

xml

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/primaryDarkColor"
    android:orientation="vertical">
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardview"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="8dp"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:padding="2dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="3dp"
                android:text="content"
                android:layout_weight="1"
                android:textColor="#ff212121"

                />


        </LinearLayout>

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


</LinearLayout>

甚至我也设置了宽度来匹配父级,但是没有用。

更新的

回收者观点

代码语言:javascript
运行
复制
<LinearLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#e5e5e5"
        android:orientation="horizontal">


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

    </LinearLayout>
    </LinearLayout>

更新:使用以下方法修正

我已经用以下答案解决了我的问题

充气的医生:

从指定的xml资源中膨胀新的视图层次结构。如果有错误,则抛出InflateException。 要加载的XML布局资源的参数资源ID (例如,R.layout.main_page)是生成的层次结构的父级(如果attachToRoot为真),或者只是一个对象,它为返回的层次结构的根提供一组LayoutParams值(如果attachToRoot为false)。attachToRoot是否应该将膨胀的层次结构附加到根参数?如果为false,根只用于为XML中的根视图创建正确的LayoutParams子类。返回充气层次结构的根视图。如果提供了根,并且attachToRoot为true,则为根;否则,它就是膨胀的XML文件的根。

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

代码语言:javascript
运行
复制
LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_listitem, parent, false);

提供父视图可以让充气器知道要使用的布局。提供false参数告诉它暂时不将其附加到父级。这就是RecyclerView会为你做的。

这个答案帮助我解决了我的问题

参考文献:parent" does not match parent RecyclerView width

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2016-11-13 17:46:43

我已经用以下答案解决了我的问题

inflate的文档

从指定的xml资源中膨胀新的视图层次结构。如果有错误,则抛出InflateException。 要加载的XML布局资源的参数资源ID (例如,R.layout.main_page)是生成的层次结构的父级(如果attachToRoot为真),或者只是一个对象,它为返回的层次结构的根提供一组LayoutParams值(如果attachToRoot为false)。attachToRoot是否应该将膨胀的层次结构附加到根参数?如果为false,根只用于为XML中的根视图创建正确的LayoutParams子类。返回充气层次结构的根视图。如果提供了根,并且attachToRoot为true,则为根;否则,它就是膨胀的XML文件的根。

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

代码语言:javascript
运行
复制
LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_listitem, parent, false);

提供parent视图可以让充气器知道要使用的布局。提供false参数告诉它暂时不将其附加到父级。这就是RecyclerView会为你做的。

这个答案帮助我解决了我的问题

参考文献:parent" does not match parent RecyclerView width

票数 7
EN

Stack Overflow用户

发布于 2021-11-11 13:57:57

在我的例子中,它适用于此更改:

代码语言:javascript
运行
复制
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FilialCruzaViewHolder {
    // Before
    // val binding = ListaFilialCruzaCardItemBinding.inflate(LayoutInflater.from(parent.context)) 

    // After
    val binding = ListaFilialCruzaCardItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    return FilialCruzaViewHolder(binding)
}
票数 1
EN

Stack Overflow用户

发布于 2016-11-13 17:09:56

在layout_margin标记中使用CardView,删除它们。您的代码应该是这样的:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primaryDarkColor"
android:orientation="vertical">
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardview"
    android:background="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="8dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:padding="2dp"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="3dp"
            android:text="content"
            android:layout_weight="1"
            android:textColor="#ff212121"

            />

    </LinearLayout>

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40576472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档