【问题标题】:Imageview with rounded corner using kotlin [duplicate]使用kotlin的带圆角的Imageview [重复]
【发布时间】:2018-12-20 06:41:34
【问题描述】:

我已经使用 Imageview 创建了布局,并设置了带有圆角半径的自定义背景,父布局是拐角的,但 Imageview 不是圆角的。我怎样才能做到这一点?

提前致谢。

bg.xml

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

    <stroke
        android:width="2dp"
        android:color="@color/colorAccent" />
    <corners
        android:topLeftRadius="20dp"
        android:topRightRadius="20dp"/>

</shape>

list_item.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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_item_bg"
    >

    <ImageView
        android:id="@+id/food_image"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="2dp"
        android:scaleType="centerCrop"
        android:clipToOutline="true"
        android:background="@drawable/image_bg"
        android:src="@drawable/sample_image"/>
</LinearLayout>

【问题讨论】:

  • 分享你的布局代码。
  • 这应该有助于link

标签: android android-layout rounded-corners


【解决方案1】:

创建 res/drawable/round_outline.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp" />
</shape>

将可绘制对象设置为 ImageView 的背景

android:background="@drawable/round_outline"

在 ImageView(布局)上

android:clipToOutline="true"

【讨论】:

  • 我已经尝试了上面的代码,但我遇到了同样的问题
  • 请提供xml布局文件
  • 我已经在我的问题中添加了代码。
  • clipToOutline 未找到 imageview 标签。所以我关注了这个库: implementation 'com.makeramen:roundedimageview:2.3.0' 。它适用于 Java 和 Kotlin。只需将您的 imageview 替换为:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-09
  • 2013-08-16
  • 2014-02-01
  • 2014-11-21
相关资源
最近更新 更多