android - PercentFrameLayout 作为 Recycler View 中的一个项目不起作用

标签 android android-recyclerview android-percent-layout

我正在使用 PercentFrameLayout 作为 RecyclerView 中的一个项目,下面是我的布局。 PercentFrameLayout 没有出现。它没有显示。

我在这里遗漏了什么......?

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

    <ImageView
        android:id="@+id/header_image_view"
        android:layout_width="match_parent"
        app:layout_heightPercent="12%"
        android:background="@drawable/placeholder_image"/>
</android.support.percent.PercentFrameLayout>

最佳答案

当您使用 layout_heightPercent 时,您要求分配给 PercentFrameLayout 的空间的百分比 - 即,如果 PercentFrameLayout 100dp 的高度,具有 layout_heightPercent="12%" 的 subview 将被赋予 12dp。由于您的 PercentFrameLayout 没有其他子项并且是 wrap_content,因此没有明确指示未指定值的 12% 是多少,导致根本没有分配空间。

如果您尝试将 View 的大小设置为总屏幕大小的百分比或 RecyclerView 可用的总可见高度的百分比,则必须在其他地方执行此操作,理想情况下作为 LinearLayoutManager 的子类,因为它控制着单个元素的高度。

关于android - PercentFrameLayout 作为 Recycler View 中的一个项目不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33603700/

相关文章:

android - 具有 DiffUtil.ItemCallback 的 ListAdapter 始终认为对象相同

android - 如何使用 Google 登录保存数据

java - View.Gone 位于适配器内部,在 onBindViewHolder 中留下空间

android - 如何设置ScrollView的起始位置?

java - 无法在 RecyclerView 中使用 ViewHolder

android - 如何为手机和平板电脑设置不同的百分比

android - 百分比框架布局 : You must supply a layout_width attribute

java - 如何在 Android(Java) 中使用 json 反斜杠

java - Firebase JSON 到 Arraylist 内的 Arraylist 存在对象问题