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

甚至我也设置了宽度来匹配父级,但是没有用。
更新的
回收者观点
<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,但要提供父级:
LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_listitem, parent, false);提供父视图可以让充气器知道要使用的布局。提供false参数告诉它暂时不将其附加到父级。这就是RecyclerView会为你做的。
这个答案帮助我解决了我的问题
发布于 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,但要提供父级:
LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_listitem, parent, false);提供parent视图可以让充气器知道要使用的布局。提供false参数告诉它暂时不将其附加到父级。这就是RecyclerView会为你做的。
这个答案帮助我解决了我的问题
发布于 2021-11-11 13:57:57
在我的例子中,它适用于此更改:
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)
}发布于 2016-11-13 17:09:56
在layout_margin标记中使用CardView,删除它们。您的代码应该是这样的:
<?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>
https://stackoverflow.com/questions/40576472
复制相似问题