[英]Android, how can I implement rounded corners for ImageButton and then change the color programatically?
I have ImageButton
on the layout of my application and I want to have rounded corners for this ImageButton
, so I'm using style like: 我的应用程序布局上有
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>
and then I'm using android:background="@drawable/my_rounded_shape.xml"/>
然后我正在使用
android:background="@drawable/my_rounded_shape.xml"/>
The problem is that after this I want to change background color from black to some custom color, but I can't modify style programmatically and there is no way to generate new style with corners but with different color and apply to my ImageButton
. 问题是在此之后,我想将背景颜色从黑色更改为某些自定义颜色,但是我无法以编程方式修改样式,并且无法生成带有角但具有不同颜色的新样式并将其应用于我的
ImageButton
。
Could you clarify is there any way to do it or any workaround? 您能否澄清有任何方法或解决方法?
Try it: Create another XML Shape With Name For Example "SS" and with your new properties 尝试:使用名称“ 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"/>
After then 然后
Button b = (Button) (findViewById(R.id.button1));
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.ss));
After some time I came up with solution for my case - in my Java code I do something like: 一段时间后,我想出了针对我的案例的解决方案-在我的Java代码中,我做了类似的事情:
GradientDrawable drawable = (GradientDrawable) imageButton.getBackground();
drawable.setColor(color);
This allows me to save rounded corners described in my "shape" and change background color at the same time. 这使我可以保存“形状”中描述的圆角并同时更改背景颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.