RecyclerView Layout Weight

文章目录

    例如,我想将 RecyclerView 的高度设置为占据整个屏幕余下的空间。

    代码结构

    // 外层,例如 LinearLayout
    android:layout_height="match_parent"
    
    // 组件,例如 RecyclerView
    android:layout_weight="1"
    android:layout_height="0dp"
    

    如果不设置 layout_height

    会遇到运行时错误:

    you must supply a layout_height attribute

    如果设置 layout_height 为 wrap_content

    若同时设置 layout_weight 和 layout_height,且 layout_height 为 wrap_content.

    会看到警告信息:

    Issue id: InefficientWeight

    Use a layout_height of 0dp instead of wrap_content for better performance When only a single widget in a LinearLayout defines a weight, it is more efficient to assign a width/height of 0dp to it since it will absorb all the remaining space anyway. With a declared width/height of 0dp it does not have to measure its own size first.