简体   繁体   中英

Unable to WRAP_CONTENT RecyclerView inside LinearLayout

Basically I have two similar layouts with different approach to create. The problem is that RecyclerView WRAP_CONTENT behaves differently on those 2 layouts. At the first layout it works fine, while at the others, WRAP_CONTENT doesn't work.

The code below doesn't work.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    ... other layout codes
    <android.support.v7.widget.RecyclerView
        android:id="@+id/xxx"
        style="@style/MarginLeftDetail"
        android:layout_width="match_parent"
        android:layout_height="0dp"
    />
     ... other layout codes
</LinearLayout>

While this is the one that is working.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    ... other layout codes
    <android.support.v7.widget.RecyclerView
        android:id="@+id/xxx"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@+id/yyy"
        style="@style/MarginLeftDetail"
        android:layout_marginTop="7.5dp"/>
    ... other layout codes
</RelativeLayout>

Does LinearLayout interpret WRAP_CONTENT differently than RelativeLayout ?

UPDATE : If I wrap the first RecyclerView with RelativeLayout , it works fine. So I guess LinearLayout really interpret WRAP_CONTENT differently? Or is it a bug?

Thanks.

You should add android:layout_weight=1 to RecyclerView if you are giving android:height="0dp" Change your code as follow

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    ... other layout codes
    <android.support.v7.widget.RecyclerView
        android:id="@+id/xxx"
        style="@style/MarginLeftDetail"
        android:layout_width="match_parent"
        android:layout_weight= "1"
        android:layout_height="0dp"
    />
     ... other layout codes
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM