我有一个drawable,我用作LinearLayout的背景。我想在运行时改变这个Shape的颜色。我曾尝试使用几种方法..但没有工作。Android:在运行时更改形状颜色
我已经按照这里介绍的方法:http://www.anddev.org/android-2d-3d-graphics-opengl-problems-f55/change-shape-drawable-solid-color-t16798.html
但有同样的问题......它不崩溃..但颜色并没有改变!
<?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();
任何线索?我已经通过了一整天google搜索......而且越来越烦...
UPDATE:
当我尝试做同样的这个形状:
<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不可变,所以你需要创建一个副本,并对其进行变异。 – Kaj 2011-05-09 18:24:17
为什么在第二个例子中变成黑色? :-S – neteinstein 2011-05-09 18:25:08
你将它设置为什么颜色?黑色? – Kaj 2011-05-09 18:51:12