美文网首页
RecyclerView中根布局使用RelativeLayout

RecyclerView中根布局使用RelativeLayout

作者: 军如令 | 来源:发表于2018-07-31 20:27 被阅读0次

在今日份的开发过程中,出现了一个Bug,头疼啊啊啊啊。

问题介绍:

在RecyclerView中的Item的根布局使用RelativeLayout布局,属性android:layout_height="wrap_content",需求是需要在部分的Item要有遮罩层,而Item的布局大小是不固定的,首先我在RelativeLayout布局中使用LinearLayout显示内容,之后在布局中添加一个ImageView来实现遮罩层。

具体布局:

    <RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginBottom="@dimen/dimen_8dp"

    android:layout_marginEnd="@dimen/dimen_16dp"

    android:layout_marginStart="@dimen/dimen_16dp"

    android:background="@drawable/seven_day_diet_item_bg"

>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

...>

<ImageView

        android:id="@+id/cover_bg"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:background="@drawable/seven_day_diet_item_cover_bg"

    />

</RelativeLayout>

然后发现ImageView没有效果,瞬间懵逼。

解决方法:

把根布局RelativeLayout替换为FrameLayout即可解决,查阅资料发现:

FrameLayout无法控制它的子控件的位置,并且FrameLayout的大小由子控件中最大的子控件决定,如果子控件都一样大的话,同一时刻只能看到最上面的组件,子控件可以通过android:layout_gravity属性控制在父布局的位置。

相关文章

网友评论

      本文标题:RecyclerView中根布局使用RelativeLayout

      本文链接:https://www.haomeiwen.com/subject/fhzhvftx.html