到目前为止,我能够做到这一点的方法是创建一个 XML 可绘制对象:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#00ffffff" />
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
<corners android:radius="12dp" />
<stroke android:width="3dp" android:color="#ffffffff" />
</shape>
在我的布局 XML 中:
<FrameLayout
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_gravity="top|center_horizontal">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/profilePicture"
android:scaleType="fitXY"
android:clickable="true"
android:contentDescription="@string/content_desc_profile_picture" />
<ImageView
android:src="@drawable/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/content_desc_default_profile_picture" />
</FrameLayout>
我的问题是框架没有覆盖其后面可绘制的 profilePicture 的角,导致实际图像超出边框。怎样才能遮住角落呢?真希望现在有一个类似 CSS 的属性!
最佳答案
形状.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#000000" />
<corners android:radius="2dp" />
</shape>
您的布局.xml
<FrameLayout
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_gravity="top|center_horizontal" xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shape"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/profilePicture"
android:scaleType="fitXY"
android:clickable="true"
android:layout_margin="5dp"
android:src="@drawable/your_image"
android:background="@drawable/shape" />
</FrameLayout>
关于java - 如何以编程方式向 ImageView 添加圆角边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39663716/