首页 > 解决方案 > 将形状设置为 ImageView 背景以获得圆角

问题描述

我正在尝试使用ConstraintLayout在ScrollView内构建带有圆角的布局。在布局的顶部,我想放置一个顶角圆角(以匹配布局角)和底角笔直的图像。

按照我在这个StackOverflow 问题中找到的建议,这是我的代码:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/scroll_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="7.5"
        android:background="@color/darkModePrimary">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="16dp"
            android:layout_width="match_parent"
            android:background="@drawable/layout_bg"
            android:layout_height="wrap_content">

            <ImageView
                android:background="@drawable/layout_bg"
                android:src="@drawable/house_medium"
                android:id="@+id/house_image"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="fitXY"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

            <TextView
                android:layout_marginTop="32dp"
                android:gravity="center"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/house_image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Some text"
                android:textSize="24sp"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

这是我为 ConstraintLayout 的背景定义形状的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/white"/>
    <corners android:radius="10dp"/>
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp" />
</shape>

如果我将此形状设置为约束布局和 ImageView 的背景,我会得到 - 正如预期的那样 - 具有所有四个圆角的图像,如此处所示。

具有四个直角的图像

我尝试创建另一个形状用作图像的背景,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/white"/>
    <corners android:bottomRightRadius="0dp"
        android:bottomLeftRadius="0dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp"/>
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp"/>
</shape>

但是当我将它设置为ImageView的背景时,我没有得到我想要的,图像的所有四个角现在都是直的

具有四个直角的图像

我究竟做错了什么?

标签: androidandroid-layoutandroid-imageviewandroid-drawableandroid-shape

解决方案


要使其正常工作,只需在代码中为您的卡片容器布局设置大纲。

  • 在布局 xml 中将 id 添加到卡片容器:

      <androidx.constraintlayout.widget.ConstraintLayout
          android:id="@+id/card"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginLeft="16dp"
          android:layout_marginTop="16dp"
          android:layout_marginRight="16dp"
          android:background="@drawable/layout_bg"
          android:elevation="4dp">
    
  • 在您的 MainActivity onCreate 中:

    override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)
    
         card.clipToOutline = true
         card.outlineProvider = ViewOutlineProvider.BACKGROUND
    }
    

结果: 在此处输入图像描述


推荐阅读