我想让我的 ImageView
中的任何图像都变成带边框的圆形。
我搜索但找不到任何有用的信息(我尝试过的任何方法都不起作用)。
如何通过 XML 实现这一点:
用特定的src创建一个ImageView
,并使其带有边框的圆形?
最佳答案
这是我设计的最简单的方式。试试这个。
依赖
实现'androidx.appcompat:appcompat:1.3.0-beta01'
实现'androidx.cardview:cardview:1.0.0'
<android.support.v7.widget.CardView android:layout_width="80dp" android:layout_height="80dp" android:elevation="12dp" android:id="@+id/view2" app:cardCornerRadius="40dp" android:layout_centerHorizontal="true" android:innerRadius="0dp" android:shape="ring" android:thicknessRatio="1.9"> <ImageView android:layout_height="80dp" android:layout_width="match_parent" android:id="@+id/imageView1" android:src="@drawable/YOUR_IMAGE" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"> </ImageView> </android.support.v7.widget.CardView>
如果您正在使用 Lollipop 以上的 android 版本
<android.support.v7.widget.CardView android:layout_width="80dp" android:layout_height="80dp" android:elevation="12dp" android:id="@+id/view2" app:cardCornerRadius="40dp" android:layout_centerHorizontal="true"> <ImageView android:layout_height="80dp" android:layout_width="match_parent" android:id="@+id/imageView1" android:src="@drawable/YOUR_IMAGE" android:scaleType="centerCrop"/> </android.support.v7.widget.CardView>
为圆形 ImageView 添加边框 - 最新版本
用另一个比内部稍大的 CardView 包裹它,并设置它的背景颜色来为你的圆形图像添加一个边框。您可以增加外部 CardView 的大小来增加边框的粗细。
<androidx.cardview.widget.CardView
android:layout_width="155dp"
android:layout_height="155dp"
app:cardCornerRadius="250dp"
app:cardBackgroundColor="@color/white">
<androidx.cardview.widget.CardView
android:layout_width="150dp"
android:layout_height="150dp"
app:cardCornerRadius="250dp"
android:layout_gravity="center">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/default_user"
android:scaleType="centerCrop"/>
</androidx.cardview.widget.CardView>
</androidx.cardview.widget.CardView>
关于android - 通过 XML 循环的 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22105775/