Last active
August 29, 2015 14:24
-
-
Save chuross/eb82ad9a9e5e6c51308b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```java | |
public static void setBackgroundColor(View view, int color) { | |
if(view == null) { | |
return; | |
} | |
// 4.2以降は対応済みなのでそのまま処理する | |
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
view.setBackgroundColor(color); | |
return; | |
} | |
// そのまま呼ぶとColorDrawableが既にセットされてた時にDrawableのmutateが呼ばれないので手動で処理する | |
if(view.getBackground() instanceof ColorDrawable) { | |
((ColorDrawable) view.getBackground().mutate()).setColor(color); | |
view.computeOpaqueFlags(); | |
} else { | |
view.setBackgroundDrawable(new ColorDrawable(color)); | |
} | |
} | |
``` | |
修正前だとColorDrawableの内部でColorStateを複製していないのでmutate呼んでももしかしたらだめかも | |
https://github.com/android/platform_frameworks_base/commit/5f49c3023a512efbef8bc9515d310c7a72be4af2#diff-b61568e8094d7de01579459b3a9fbd55R76 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```java | |
public static void setBackgroundColor(View view, int color) { | |
if(view == null) { | |
return; | |
} | |
// 4.2以降は対応済みなのでそのまま処理する | |
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
view.setBackgroundColor(color); | |
return; | |
} | |
view.setBackgroundDrawable(new ColorDrawable(color)); | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment