android - 无法在 recyclerview 中获取行的圆角

标签 android android-recyclerview rounded-corners

我正在开发一个 Android 应用程序,我正在使用 recyclerview。我已经设法显示带有数据的行并对其应用设计,但我无法为行设置圆角。下面是我的 xml 文件。

名称为 rounded_corners_cards.xml 的圆角文件:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/white"/>

    <stroke android:width="3dp"
        android:color="@color/grayLight"/>

    <padding android:left="1dp"
        android:top="1dp"
        android:right="1dp"
        android:bottom="1dp"/>

    <corners android:bottomRightRadius="7dp"
        android:bottomLeftRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp"/>
</shape> 

行文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/StandardListRow"
    android:layout_height="60dp"
    android:orientation="vertical"
    android:background="@drawable/rounded_corners_cards">

    <ImageView
        android:id="@+id/offer_picture"
        style="@style/StyleRowIcon"
        android:contentDescription="@string/card_offer_image"
        android:scaleType="centerInside" />
</RelativeLayout>

我尝试更改 rounded_corners_cards.xml 文件,但没有成功。

结果如下图所示: enter image description here

感谢您的建议。

最佳答案

到目前为止,在布局上获得圆角的最简单方法是用 CardView 包裹它。通常,这将显示阴影,但您可以使用 cardElevation 属性将其关闭。

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/StandardListRow"
    android:layout_height="60dp"
    app:cardCornerRadius="7dp"
    app:cardElevation="0dp">

    <ImageView
        android:id="@+id/offer_picture"
        style="@style/StyleRowIcon"
        android:contentDescription="@string/card_offer_image"
        android:scaleType="centerInside" />
</android.support.v7.widget.CardView>

注意:这仅适用于 Lollipop+

Due to expensive nature of rounded corner clipping, on platforms before Lollipop, CardView does not clip its children that intersect with rounded corners.

关于android - 无法在 recyclerview 中获取行的圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48602246/

相关文章:

css - 文本环绕在圆 Angular 框中

html - 有边框半径但没有边框的 DIV

java - IntentService 失去与互联网的连接

java - 无法限制 Android 应用程序的 google Api key

java - 不仅为我的帐户获取 Facebook 生日

android-recyclerview - 检查项目是否在 RecyclerView 中完全可见

android - 如何将 OnClickListener 添加到 CardView?

Android 如何在单击 RecyclerView 项目时从 fragment 发送 Intent

android - 强制字体大小

CSS圆 Angular 表格行的上 Angular