【问题标题】:Can't get rounded corners of row in recyclerview无法在 recyclerview 中获得行的圆角
【发布时间】:2020-01-28 21:10:27
【问题描述】:

我正在开发一个 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 文件但没有运气。

结果如下图所示:

感谢您的建议。

【问题讨论】:

  • 能否添加一张你得到的结果的图片?
  • 我更新了问题并添加了截图。
  • 添加你的样式文件。我正在测试,我怀疑宽度和高度。

标签: android android-recyclerview rounded-corners


【解决方案1】:

到目前为止,在布局上获得圆角的最简单方法是用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+

由于圆角剪裁成本高昂,在 Lollipop 之前的平台上,CardView 不会剪裁与圆角相交的子项。

【讨论】:

  • 我已经尝试过了,但仍然得到相同的结果。
  • 感谢世界上每一位 iOS 程序员的精彩提示! :) 在机器人上很容易
【解决方案2】:

上面提到的最简单的方法是使用cardView https://developer.android.com/reference/android/support/v7/widget/CardView.html

只需将其用作您的 RecyclerView 项目 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:contentPadding="10dp"
    app:cardElevation="6dp"
    app:cardCornerRadius="10dp"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="10dp"
    android:background="@color/cardview_light_background">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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


    </LinearLayout>

</android.support.v7.widget.CardView>


</LinearLayout>

注意:- cardCornerRadius 是定义卡片圆度的元素,dp 越高,越圆。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    相关资源
    最近更新 更多