我有一個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