我尝试将 ImageView 显示为 RecyclerView 元素内的正方形,无论元素的高度是多少。
这是我想要的:
这是我的 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>
(如果我的英语不好,请原谅)
最佳答案
您需要将百分比布局在层次结构中向上移动一步:
<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/