使用Drawable资源文件设置ImageView圆角描边失败的解决咨询
问题解决方法
核心错误分析
你把描边背景Drawable错误设置成了ImageView的前景图片(通过setImageResource),而非背景,同时你的Drawable资源文件缺少必要的<shape>根标签,这两个问题导致描边无法正常显示。
具体修复步骤
完善Drawable资源文件
你的media_contour_background.xml必须用<shape>标签包裹,否则Android无法识别为形状Drawable,同时建议补充dashGap属性让虚线显示正常:<shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="@dimen/_50sdp" /> <stroke android:width="@dimen/_10sdp" android:color="@color/lagfo_color" android:dashWidth="@dimen/_10sdp" android:dashGap="@dimen/_5sdp" /> </shape>正确设置背景
将描边Drawable设置为ImageView的背景,而非前景图片:binding.photo.setBackgroundResource(R.drawable.media_contour_background)如果需要在ImageView中显示实际图片,再通过
setImageResource/setImageURI等方法设置图片资源,比如:binding.photo.setImageResource(R.drawable.your_actual_image)优化圆角匹配(可选)
若要让图片也跟随背景圆角裁剪,避免超出描边范围,给ImageView添加属性:android:clipToOutline="true"
内容的提问来源于stack exchange,提问作者Patrick Renaud
相关产品推荐
相关产品推荐

