我在这里看到了一些关于圆角和彩色背景的东西,但没有一个完全适合我的需求。
我看到的制作圆角按钮(有效)的方法是在可绘制文件夹中创建一个 xml 文件,然后放置类似的东西。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/Blue"/>
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
这很好,只需一个按钮。 有没有办法将四种颜色组合成一个 XML 文件,这样我就不必制作四种单独的颜色?我希望它们都有像这个蓝色按钮一样的 10dp 半径,但每个都有自己各自的颜色。
我问的是我可以获得圆角边缘的唯一方法是在我的主 XML 文件中说是android:background="@drawable/round
如果我能这么说然后android:background="@color/Blue"
那就好了,但由于我无法调用android:background
我不得不在这个可绘制的 XML 文件中命名颜色。所以现在我不知道如何动态更改此轮.xml文件中的颜色 问候。
您可以通过以下方式定义一个 xml 并动态更改按钮颜色:
GradientDrawable sd = (GradientDrawable) button1.getBackground();
sd.setColor(Color.BLUE);
GradientDrawable sd1 = (GradientDrawable) button2.getBackground();
sd1.setColor(Color.RED);
您可以将按钮的当前背景作为可绘制对象获取,然后使用以下代码设置色调以更改其颜色:
Drawable drawable = button1.getBackground();
DrawableCompat.setTint(drawable, Color.BLACK);