You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ShapeableImageView添加TextView后底部圆角半径显示异常问题

问题描述

我使用了圆角半径为28dp的MaterialCardView,希望内部的ShapeableImageView四个角都设置为相同的28dp圆角。未添加TextView时,两者显示正常;但在ShapeableImageView下方添加TextView后,ShapeableImageView的底部圆角显示异常,看起来并非28dp。我通过自定义样式@style/HomeCardShape为ShapeableImageView设置圆角,相关代码如下:

布局代码

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".com.example.TestActivity">
    
    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="32dp"
        app:cardCornerRadius="28dp"
        app:strokeWidth="3dp"
        app:strokeColor="@color/colorPrimary"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.imageview.ShapeableImageView
                android:id="@+id/shapeableImageView2"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                app:layout_constraintDimensionRatio="16:10"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:shapeAppearanceOverlay="@style/HomeCardShape"
                app:srcCompat="@drawable/card_test_image" />

            <TextView
                android:id="@+id/textView16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="1dp"
                android:text="TextView"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/shapeableImageView2" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </com.google.android.material.card.MaterialCardView>
    
</androidx.constraintlayout.widget.ConstraintLayout>

样式代码

<style name="HomeCardShape">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">28dp</item> 
</style

解决方案

1. 修复样式语法错误

样式代码中</style>标签不完整,缺少闭合的>,修正后:

<style name="HomeCardShape">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">28dp</item> 
</style>

2. 调整ShapeableImageView布局属性

  • 移除android:adjustViewBounds="true":该属性与app:layout_constraintDimensionRatio冲突,会干扰尺寸计算
  • 添加底部约束:将ShapeableImageView的底部与TextView顶部绑定,确保高度计算稳定
  • 优化CardView内边距:关闭兼容内边距并设置内边距为0,避免内部布局与CardView圆角边界错位

修正后的完整布局代码:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".com.example.TestActivity">
    
    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="32dp"
        android:padding="0dp"
        app:cardCornerRadius="28dp"
        app:cardUseCompatPadding="false"
        app:strokeWidth="3dp"
        app:strokeColor="@color/colorPrimary"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.imageview.ShapeableImageView
                android:id="@+id/shapeableImageView2"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:scaleType="centerCrop"
                app:layout_constraintDimensionRatio="16:10"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toTopOf="@+id/textView16"
                app:shapeAppearanceOverlay="@style/HomeCardShape"
                app:srcCompat="@drawable/card_test_image" />

            <TextView
                android:id="@+id/textView16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="16dp"
                android:text="TextView"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/shapeableImageView2" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </com.google.android.material.card.MaterialCardView>
    
</androidx.constraintlayout.widget.ConstraintLayout>

3. 效果说明

修正后,ShapeableImageView的四个角将稳定显示28dp圆角,不会因添加TextView而出现异常。核心原因是解决了布局约束冲突,确保ImageView的尺寸计算准确,圆角渲染不受布局变化影响。


内容的提问来源于stack exchange,提问作者adrian joshua

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 17:57:01