如何制作带背景的圆形ImageView布局?现有实现效果不佳求助
嘿,我一眼就看出问题所在啦!你给CircleImageView设置的宽高不一致(250dp宽、150dp高),这肯定没法显示成正圆形,只会变成椭圆。另外RelativeLayout里的几个居中属性有点重复,咱们来优化下:
正确实现方案
首先梳理下核心问题:
- CircleImageView要显示正圆形,宽高必须完全相等,否则会被拉伸成椭圆
- RelativeLayout内的居中属性冗余,无需重复设置多个
调整后的代码如下:
<RelativeLayout android:id="@+id/relative" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/dark_blue" android:paddingBottom="20dp"> <!-- 增加底部内边距,避免图片贴紧布局底部 --> <de.hdodenhof.circleimageview.CircleImageView android:id="@+id/circleImageView" android:layout_width="150dp" android:layout_height="150dp" <!-- 宽高设为一致,保证圆形显示 --> android:layout_centerInParent="true" <!-- 单个属性即可实现水平+垂直居中 --> android:layout_marginTop="20dp" android:src="@drawable/ic_default_profile" /> </RelativeLayout>
关键细节说明:
- 同步修改CircleImageView的宽高数值(比如都设为150dp),这样才能呈现标准圆形;如果需要更大的圆形,只要保持宽高一致即可
- 移除了重复的
layout_centerHorizontal="true"和layout_gravity="center",因为layout_centerInParent="true"已经包含了水平与垂直居中的效果 - 给RelativeLayout添加
paddingBottom,让图片上下的间距更对称,视觉体验更好
内容的提问来源于stack exchange,提问作者CoderMohammad
相关产品推荐
相关产品推荐

