Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
778 views
in Technique[技术] by (71.8m points)

android - make RecyclerView's height to "wrap_content" in Constraint layout

i'm trying to set the height of recycler view on wrap_content and make it respect that,but it will exceed the other widget on layout. what can i do now?

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white">



    <TextView
        android:id="@+id/tvPastRounds"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="0dp"
        android:text="Past Rounds"
        android:textColor="@color/text_color_black"
        android:textSize="16sp"
        app:layout_constraintLeft_toLeftOf="parent"/>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="24dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="16dp"
        android:clipChildren="true"
        android:maxHeight="150dp"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/tvPastRounds"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvPastRounds" app:layout_constraintVertical_bias="0.0"/>
</android.support.constraint.ConstraintLayout>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

i had the same issue and i find this solution: you should add this attribute to your recyclerview and it makes your recyclerview wrap_content in the constraint_layout:

app:layout_constraintHeight_default="wrap"

let me know if this solution fixed your problem.

EDIT : recycler's height should be 0dp.

EDIT 2 : in the newer versions of support library, use this code:

android:layout_height="wrap_content"
app:layout_constrainedHeight="true"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...