android - RecyclerView 中的 PercentFrameLayout (要求方形)未显示

标签 android xml android-recyclerview square android-percent-library

我尝试将 ImageView 显示为 RecyclerView 元素内的正方形,无论元素的高度是多少。
这是我想要的:
ImageView in a squared form in a recycler view

这是我的 xml:

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

    <android.support.percent.PercentFrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        >
        <ImageView
            android:scaleType="centerCrop"
            app:layout_heightPercent="100%"
            app:layout_aspectRatio="100%"/>
    </android.support.percent.PercentFrameLayout>

    <!-- other views -->
</RelativeLayout>

这是我得到的(ImageView 消失了): wrong design

(如果我的英语不好,请原谅)

最佳答案

您需要将百分比布局在层次结构中向上移动一步:

<android.support.percent.PercentRelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <ImageView
        android:scaleType="centerCrop"
        app:layout_heightPercent="100%"
        app:layout_aspectRatio="100%"/>

    <!-- other views -->
</android.support.percent.PercentRelativeLayout>

此方法假设您的其他 View 具有设定的高度 - 如果它们具有可变的高度,那么您的图像也将具有可变的高度(因为它使用 layout_heightPercent="100%".

如果您希望图像始终保持特定尺寸,则需要诸如

<android.support.percent.PercentRelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <ImageView
        android:scaleType="centerCrop"
        app:layout_widthPercent="20%"
        app:layout_aspectRatio="100%"/>

    <!-- other views -->
</android.support.percent.PercentRelativeLayout>

在这种情况下,宽度将是总宽度的固定百分比(您也可以使用layout_width="@dimen/fixed_width"),并且高度将等于该宽度。

关于android - RecyclerView 中的 PercentFrameLayout (要求方形)未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38085262/

相关文章:

java - 读取 QueueFile 磁带时出现 EOFException

Android设备驱动程序制作节点

安卓 : E/InputDispatcher: Window handle drag has no registered input channel

java - Android检查多个字符串是否不为空

Python 读取 XML 文件时不断出现错误 "list index out of range"

c# - 从后代获取 xml 元素列表

php - 我的 Activity 页面没有结果

java - 回收器 View 不显示 fragment 中的项目

Android RecyclerView : Change layout file LIST to GRID onOptionItemSelected

java - 如何以编程方式更改 RecyclerView 项目属性,特别是 recyclerview 项目内的自定义 View