【发布时间】:2017-07-21 12:44:33
【问题描述】:
我的问题与this question 非常相似,但与OP 不同的是,我将parent 传递给LayoutInflater。我正在使用支持库 25.3.1。这是我正在膨胀的布局(将 ConstraintLayout 更改为 LinearLayout 没有帮助):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="72dp"
android:background="?android:selectableItemBackground"
android:clickable="true"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/list_text_primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:singleLine="true"
android:textAppearance="@style/TextAppearance.ListPrimary"
app:layout_constraintBottom_toTopOf="@+id/list_text_secondary"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
tools:ignore="Deprecated"
tools:text="list_text_primary"/>
<TextView
android:id="@+id/list_text_secondary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:gravity="center_vertical"
android:singleLine="true"
android:textAppearance="@style/TextAppearance.ListSecondary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/list_text_primary"
tools:ignore="Deprecated"
tools:text="list_text_secondary"/>
</android.support.constraint.ConstraintLayout>
我还尝试在onCreateViewHolder 中手动设置LayoutParams,如here 所述。我认为它有效,但不时parent 的宽度和高度为 0(我无法查明原因),导致孩子变得不可见。 根据this answer 的说法,RecyclerView 应该在我调用 onCreateViewHolder 的时候已经测量过了。
【问题讨论】:
-
我遇到了同样的问题。你找到解决办法了吗?
标签: android android-layout android-recyclerview android-support-library