【发布时间】:2011-08-21 21:36:55
【问题描述】:
我有一个可绘制对象,用作线性布局的背景。我想在运行时更改此形状的颜色。我尝试了几种方法..但没有一个工作。
但是有同样的问题...它不会崩溃..但是颜色没有改变!
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00A6C1" />
<corners android:radius="@dimen/square_corners" />
</shape>
代码片段:
GradientDrawable drawable = (GradientDrawable) activity.getResources().getDrawable(R.drawable.blue_square_shape);
int color = ((Application) getApplication()).getColor();
drawable.setColor(color);
block.findViewById(R.id.blockSquare).setBackgroundDrawable(drawable);
findViewById(R.id.blockSquare).postInvalidate();
有什么线索吗?我已经通过了一整天的谷歌搜索......而且它变得非常烦人......
更新:
当我尝试对这个形状做同样的事情时:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/shape" android:shape="rectangle">
<gradient android:startColor="#1FBCCF" android:endColor="#06A4C1"
android:angle="270" />
<corners android:topLeftRadius="@dimen/footer_corners"
android:topRightRadius="@dimen/footer_corners" />
</shape>
颜色变成黑色...我猜它可以改变...
【问题讨论】:
-
只是猜测。 Drawable 是不可改变的,所以你需要创建一个副本,然后改变那个副本。
-
为什么在第二个例子中它变成黑色?! :-S
-
你设置成什么颜色?黑色?
-
没有。黄色的。我注意到的一件事是我传递的颜色是 RGB,它需要 ARGB。我使用的颜色是对字符串#RRGGBB 的解析。
-
在这种情况下,请使用已解析数字和 0xFF000000 的二进制 OR。