【问题标题】:Horizontal RecyclerView not wrapping layout水平 RecyclerView 不环绕布局
【发布时间】:2016-07-16 16:54:53
【问题描述】:

每个项目应覆盖屏幕宽度的 70%。虽然 70% 的屏幕被填满,但我得到了 30% 的空白空间,下一个项目在它之后开始。 这是布局 -

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="4"
        app:cardCornerRadius="2dp"
        app:cardElevation="2dp"
        app:cardPreventCornerOverlap="false"
        app:cardUseCompatPadding="true"
        app:contentPadding="2dp"
        app:layout_widthPercent="70%">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/image_test"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"/>

        </FrameLayout>
    </android.support.v7.widget.CardView>
</android.support.percent.PercentRelativeLayout>

【问题讨论】:

  • 尝试将app:layout_widthPercent="70%"设置为您的PercentRelativeLayout并将CardView设置为match_parent
  • 问题不在于这个布局,而在于包含recyvlerview的布局。如果您在其中任何地方添加了 layout_margintop 任何类似的内容,请删除它
  • @IvanIvanov 如何为 PercentRelativeLayout 本身提供百分比宽度。我上次检查百分比是相对的。

标签: android android-recyclerview linearlayoutmanager


【解决方案1】:

之前Android Support Library, revision 23.2.1recyclerView 没有考虑他的itemView 的height(vertical) 或width(Horizo​​ntal)。它总是把它包起来。

但现在它考虑高度或宽度。在你的情况下,它是宽度。

所以,让你的 layout_widthmatch_parentwrap_content

【讨论】:

  • recyclerview 的layout_width ?
  • 子项是PercentRelativeLayout,使其包裹不会使其布局占其父项的 70%。它的match_parent 是有原因的,这样子项可以占据屏幕的 70%。
  • 在这种情况下,您需要解决问题,因为它不知道 match_parent 的大小。设置 match_parent 时可能会采用默认的最大宽度。您可以手动计算百分比并将其设置为您在 onCreateVeiwHolder 中的 cardView。
猜你喜欢
  • 1970-01-01
  • 2018-04-08
  • 2019-01-20
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-14
相关资源
最近更新 更多