假设我有一个 ImageView
定义为:
<ImageView
android:id="@+id/my_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/my_image_view_background"
/>
my_image_view_background
为:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<gradient
android:angle="45"
android:startColor="#555555"
android:endColor="#111111"
android:type="radial"
android:centerX="0.1"
android:centerY="0.1"
android:gradientRadius="70"
/>
<stroke
android:width="5dp"
android:color="#ff3300"
/>
</shape>
是否可以访问 my_image_view_background
可绘制对象并在代码中更改其描边的颜色?另外,是否可以根据“状态”(按下/选择/默认)来做到这一点?
最佳答案
是的,这是可能的
使用 GradientDrawable,您可以通过代码来完成此操作。
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {0xFFadced4, 0xFF71c2eb });
gd.setCornerRadius(0f);
gd.setStroke(1, 0xFF7c279b); //here you can define stroke width
gd.setCornerRadius(3f);
关于java - 通过代码访问可绘制形状的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9911677/