Java Android,我如何实现ImageButton的圆角,然后通过编程更改颜色?
我在应用程序的布局上有Java Android,我如何实现ImageButton的圆角,然后通过编程更改颜色?,java,android,android-styles,android-imagebutton,Java,Android,Android Styles,Android Imagebutton,我在应用程序的布局上有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
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\u rounded\u shape.xml”/>
问题是,在此之后,我想将背景颜色从黑色更改为一些自定义颜色,但我无法通过编程方式修改样式,也无法生成具有不同颜色的角的新样式,并应用于我的ImageButton
你能澄清一下有什么办法或解决办法吗?试试:
创建另一个名为“SS”的XML形状
还有你的新房子
<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"/>
过了一段时间,我为我的案例想出了解决方案——在我的Java代码中,我做了如下工作:
GradientDrawable drawable = (GradientDrawable) imageButton.getBackground();
drawable.setColor(color);
这允许我保存“形状”中描述的圆角,同时更改背景颜色 这看起来很像你的问题谢谢,但这种方法实际上不允许动态改变颜色(这就是我需要的)
GradientDrawable drawable = (GradientDrawable) imageButton.getBackground();
drawable.setColor(color);