int[][] states = new int[][] { new int[] { android.R.attr.state_enabled}, // enabled new int[] {-android.R.attr.state_enabled}, // disabled new int[] {-android.R.attr.state_checked}, // unchecked new int[] { android.R.attr.state_pressed} // pressed }; int[] colors = new int[] { Color.BLACK, Color.RED, Color.GREEN, Color.BLUE }; ColorStateList myList = new ColorStateList(states, colors);
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AppCompatButton v = (AppCompatButton) findViewById(R.id.mybutton); ColorStateList csl = new ColorStateList(new int[][]{new int[0]}, new int[]{0xffffcc00}); v.setSupportBackgroundTintList(csl); } }
ColorStateList myColorStateList = new ColorStateList( new int[][]{ new int[]{android.R.attr.state_pressed}, //1 new int[]{android.R.attr.state_focused}, //2 new int[]{android.R.attr.state_focused, android.R.attr.state_pressed} //3 }, new int[] { Color.RED, //1 Color.GREEN, //2 Color.BLUE //3 } );
ColorStateList myColorStateList = new ColorStateList( new int[][]{ new int[]{android.R.attr.state_pressed}, new int[]{} }, new int[] { context.getResources().getColor(R.color.white), context.getResources().getColor(R.color.black), } );
new ColorStateList( new int[][] { new int[]{} }, new int[] { pressedColor } );
/** * helper to create a colorStateList for the text * * @param text_color * @param selected_text_color * @return */ public static ColorStateList getTextColorStateList(int text_color, int selected_text_color) { return new ColorStateList( new int[][]{ new int[]{android.R.attr.state_selected}, new int[]{} }, new int[]{ selected_text_color, text_color } ); }
if(Build.VERSION.SDK_INT>=21) { ColorStateList colorStateList = new ColorStateList( new int[][]{ new int[]{-android.R.attr.state_enabled}, //disabled new int[]{android.R.attr.state_enabled} //enabled }, new int[] { Color.BLACK //disabled ,Color.BLUE //enabled } ); radio.setButtonTintList(colorStateList);//set the color tint list radio.invalidate(); //could not be necessary }
private ColorStateList getSwitchTrackColorStateList() { if (mSwitchTrackStateList == null) { final int[][] states = new int[3][]; final int[] colors = new int[3]; int i = 0; // Disabled state states[i] = new int[] { -android.R.attr.state_enabled }; colors[i] = getThemeAttrColor(android.R.attr.colorForeground, 0.1f); i++; states[i] = new int[] { android.R.attr.state_checked }; colors[i] = getThemeAttrColor(R.attr.colorControlActivated, 0.3f); i++; // Default enabled state states[i] = new int[0]; colors[i] = getThemeAttrColor(android.R.attr.colorForeground, 0.3f); i++; mSwitchTrackStateList = new ColorStateList(states, colors); } return mSwitchTrackStateList; }
ColorStateList buttonStates = new ColorStateList( new int[][]{ new int[]{android.R.attr.state_checked}, new int[]{-android.R.attr.state_enabled}, new int[]{} }, new int[]{ Color.RED, Color.BLUE, Color.GREEN } ); switchInput.setButtonTintList(buttonStates);
/** * 颜色选择器 * * @param pressedColor 按下的颜色 * @param normalColor 正常的颜色 * @return 颜色选择器 */ public static ColorStateList getColorStateList(int pressedColor, int normalColor) { //其他状态默认为白色 return new ColorStateList( new int[][]{{android.R.attr.state_enabled, android.R.attr.state_pressed}, {android.R.attr.state_enabled}, {}}, new int[]{pressedColor, normalColor, Color.WHITE}); }
AppCompatRadioButton rb; rb = new AppCompatRadioButton(mContext); ColorStateList colorStateList = new ColorStateList( new int[][]{ new int[]{-android.R.attr.state_checked}, new int[]{android.R.attr.state_checked} }, new int[]{ Color.DKGRAY , Color.rgb (242,81,112), } ); rb.setSupportButtonTintList(colorStateList);
public static void setFabBackgroundTint(FloatingActionButton fab, int color) { ColorStateList fabColorStateList = new ColorStateList( new int[][]{ new int[]{} }, new int[]{ color, } ); fab.setBackgroundTintList(fabColorStateList); } }
@Override public void onAnimationUpdate(ValueAnimator animation) { ViewCompat.setBackgroundTintList(view, new ColorStateList(EMPTY, new int[]{(int) animation.getAnimatedValue()})); } });
@Override public void onAnimationEnd(Animator animation) { ViewCompat.setBackgroundTintList(view, new ColorStateList(EMPTY, new int[]{colorEnd})); }
RippleDrawable rippleDrawable = (RippleDrawable)view.getBackground(); // assumes bg is a RippleDrawable int[][] states = new int[][] { new int[] { android.R.attr.state_enabled} }; int[] colors = new int[] { Color.BLUE }; // sets the ripple color to blue ColorStateList colorStateList = new ColorStateList(states, colors); rippleDrawable.setColor(colorStateList);
String filter = ...; String itemValue = ...; int startPos = itemValue.toLowerCase(Locale.US).indexOf(filter.toLowerCase(Locale.US)); int endPos = startPos + filter.length(); if (startPos != -1) // This should always be true, just a sanity check { Spannable spannable = new SpannableString(itemValue); ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] { Color.BLUE }); TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null); spannable.setSpan(highlightSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(spannable); } else textView.setText(itemValue);
private void applyActiveColor(@ColorInt int activeColor) { if (mDotPaint != null) { mDotPaint.setColor(activeColor); } ColorStateList textViewColorStateList = new ColorStateList( new int[][]{ new int[]{android.R.attr.state_selected}, new int[]{0} }, new int[]{ activeColor, ContextCompat.getColor(getContext(), R.color.ucrop_color_widget) } ); setTextColor(textViewColorStateList); }
static ColorStateList bootstrapDropDownViewText(Context context) { int defaultColor = ColorUtils.resolveColor(R.color.bootstrap_gray_dark, context); int activeColor = ColorUtils.resolveColor(android.R.color.black, context); int disabledColor = ColorUtils.resolveColor(R.color.bootstrap_gray_light, context); return new ColorStateList(getStateList(), getColorList(defaultColor, activeColor, disabledColor)); }
@Test public void testColorStateList() { int[][] states = {{0}}; int[] colors = {Color.GREEN}; ColorStateList colorStateList = new ColorStateList(states, colors); TextDrawable drawable = getMountedDrawableForTextWithColors( "Some text", 0, colorStateList); assertThat(drawable.getColor()).isEqualTo(Color.GREEN); }
@Test public void testColorOverride() { int[][] states = {{0}}; int[] colors = {Color.GREEN}; ColorStateList colorStateList = new ColorStateList(states, colors); TextDrawable drawable = getMountedDrawableForTextWithColors( "Some text", Color.RED, colorStateList); assertThat(drawable.getColor()).isEqualTo(Color.RED); }