iCo*_*ers 1 android android-recyclerview
我正在使用 recylcer 视图,每一行都包含 cardview。即使我的 textview 内容较少,我也需要显示 cardview 全宽。我尝试了很多方法但没有用。
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>
Run Code Online (Sandbox Code Playgroud)
即使我设置了宽度以匹配父级但没有用。
更新
回收者视图
<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>
Run Code Online (Sandbox Code Playgroud)
更新:使用以下方法修复
我已使用以下答案解决了我的问题
膨胀的文档:
从指定的 xml 资源膨胀一个新的视图层次结构。如果有错误,则抛出 InflateException。
要加载的 XML 布局资源的参数资源 ID(例如,R.layout.main_page)作为生成层次结构的父视图的根视图(如果 attachToRoot 为真),或者只是一个为根提供一组 LayoutParams 值的对象返回的层次结构(如果 attachToRoot 为 false。) attachToRoot 膨胀的层次结构是否应该附加到根参数?如果为 false,则 root 仅用于为 XML 中的根视图创建 LayoutParams 的正确子类。返回膨胀层次结构的根视图。如果提供了 root 并且 attachToRoot 为真,则这是 root;否则它是膨胀的 XML 文件的根。
这里重要的是不要提供 true,但要提供父级:
LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_listitem, parent, false);
Run Code Online (Sandbox Code Playgroud)
提供父视图让充气器知道要使用什么布局参数。提供 false 参数告诉它暂时不要将它附加到父级。这就是 RecyclerView 将为您做的事情。
这个答案帮助我解决了我的问题
参考: CardView layout_width="match_parent" 与父 RecyclerView 宽度不匹配
我已使用以下答案解决了我的问题
的文档inflate:
从指定的 xml 资源膨胀一个新的视图层次结构。如果有错误,则抛出 InflateException。
要加载的 XML 布局资源的参数资源 ID(例如,R.layout.main_page)作为生成层次结构的父视图的根视图(如果 attachToRoot 为真),或者只是一个为根提供一组 LayoutParams 值的对象返回的层次结构(如果 attachToRoot 为 false。) attachToRoot 膨胀的层次结构是否应该附加到根参数?如果为 false,则 root 仅用于为 XML 中的根视图创建 LayoutParams 的正确子类。返回膨胀层次结构的根视图。如果提供了 root 并且 attachToRoot 为真,则这是 root;否则它是膨胀的 XML 文件的根。
这里重要的是不要提供 true,但要提供父级:
LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_listitem, parent, false);
Run Code Online (Sandbox Code Playgroud)
提供parent视图让充气器知道要使用什么布局参数。提供 false 参数告诉它暂时不要将它附加到父级。这就是 RecyclerView 将为您做的事情。
这个答案帮助我解决了我的问题
参考:CardView layout_width="match_parent" 与父 RecyclerView 宽度不匹配
| 归档时间: |
|
| 查看次数: |
2746 次 |
| 最近记录: |