我的应用程序布局上有 ImageButton
,并且我希望此 ImageButton
具有圆角,因此我使用如下样式:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp"
android:color="@android:color/black" />
<corners android:radius="15dp" />
<solid android:color="@android:color/black"/>
</shape>
然后我使用 android:background="@drawable/my_rounded_shape.xml"/>
问题是,在此之后我想将背景颜色从黑色更改为某种自定义颜色,但我无法以编程方式修改样式,并且无法生成带有角但颜色不同的新样式并应用于我的 图像按钮
。
您能否澄清一下是否有任何方法可以做到这一点或有任何解决方法?
最佳答案
尝试一下: 创建另一个 XML 形状,其名称例如“SS” 以及您的新属性
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp"
android:color="@android:color/red" />
<corners android:radius="10dp" />
<solid android:color="@android:color/blue"/>
之后
Button b = (Button) (findViewById(R.id.button1));
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.ss));
关于java - Android,如何实现 ImageButton 的圆角,然后以编程方式更改颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26204201/