【问题标题】:Unable to WRAP_CONTENT RecyclerView inside LinearLayout无法在 LinearLayout 内 WRAP_CONTENT RecyclerView
【发布时间】:2016-11-05 17:18:53
【问题描述】:

基本上,我有两个相似的布局,但创建方法不同。问题是 RecyclerView WRAP_CONTENT 在这两种布局上的行为不同。在第一个布局中它可以正常工作,而在其他布局中,WRAP_CONTENT 不起作用。

下面的代码不起作用。

<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>

虽然这是一个有效的方法。

<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>

LinearLayoutWRAP_CONTENT 的解释是否与 RelativeLayout 不同?

更新:如果我用RelativeLayout 包装第一个RecyclerView,它工作正常。所以我猜LinearLayout 真的对WRAP_CONTENT 有不同的解释吗?还是bug?

谢谢。

【问题讨论】:

  • 您必须提供更多问题详情。您可以发布线性和相对布局的屏幕截图吗?

标签: android android-layout android-recyclerview android-linearlayout


【解决方案1】:

如果您提供android:height="0dp",则应将android:layout_weight=1 添加到RecyclerView 如下更改您的代码

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 1970-01-01
    相关资源
    最近更新 更多