下面列出了怎么用android.content.res.ColorStateList的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onCreate() {
super.onCreate();
// SpInstance.getInstance().init(getApplicationContext(),"skin_sp_file",false);
AndroidSkin.getInstance().init(this);
AndroidSkin.getInstance().addCustomAttrSupport(R.attr.tv_border_color,
new ICustAttrApplyForColorListener<TestView>() {
@Override
public void applyColor(int attrId, TestView view, ColorStateList color) {
view.setBorderColor(color.getDefaultColor());
}
});
AndroidSkin.getInstance().addCustomAttrSupport(R.attr.tv_drawable_left_and_right,
new TestViewDrawableLRListener());
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private ColorStateList getColorSelector(int normalColor, int pressedColor, int disabledColor, int selectedColor, int checkedColor) {
return new ColorStateList(
new int[][]{
new int[]{mStatePressed},
new int[]{mStateSelected},
new int[]{mStateChecked},
new int[]{mStateDisabled},
new int[]{}
},
new int[]{
pressedColor,
selectedColor,
checkedColor,
disabledColor,
normalColor
}
);
}
private void changeSecondaryColor() {
//disable search mode for tabLayout
disableSearchMode();
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setSelectedTabIndicatorColor(mSecondaryColor);
mFab.setBackgroundTintList(ColorStateList.valueOf(mSecondaryColor));
if (rightDrawer != null) {
mColorItem2.withIconColor(mSecondaryColor);
rightDrawer.updateItem(mColorItem2);
}
RecyclerOnClickListener.setSecondaryColor(mSecondaryColor);
}
@Override
public void loadData() {
RetrofitHelper.getBiliAppAPI()
.getVideoDetails(av)
.compose(this.bindToLifecycle())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(videoDetails -> {
mVideoDetailsInfo = videoDetails.getData();
LogUtil.test(" VideoDetails finishTask" + mVideoDetailsInfo.getTitle());
finishTask();
}, throwable -> {
mFAB.setClickable(false);
mFAB.setBackgroundTintList(ColorStateList.valueOf(
getResources().getColor(R.color.gray_20)));
});
}
@Test
public void testPlaceholderTextColor() {
ReactEditText view = mManager.createViewInstance(mThemedContext);
final ColorStateList defaultPlaceholderColorStateList =
DefaultStyleValuesUtil.getDefaultTextColorHint(
view.getContext());
ColorStateList colors = view.getHintTextColors();
assertThat(colors).isEqualTo(defaultPlaceholderColorStateList);
mManager.updateProperties(view, buildStyles("placeholderTextColor", null));
colors = view.getHintTextColors();
assertThat(colors).isEqualTo(defaultPlaceholderColorStateList);
mManager.updateProperties(view, buildStyles("placeholderTextColor", Color.RED));
colors = view.getHintTextColors();
assertThat(colors.getDefaultColor()).isEqualTo(Color.RED);
mManager.updateProperties(view, buildStyles("placeholderTextColor", null));
colors = view.getHintTextColors();
assertThat(colors).isEqualTo(defaultPlaceholderColorStateList);
}
private CalendarItemStyle(
ColorStateList backgroundColor,
ColorStateList textColor,
ColorStateList strokeColor,
int strokeWidth,
ShapeAppearanceModel itemShape,
@NonNull Rect insets) {
Preconditions.checkArgumentNonnegative(insets.left);
Preconditions.checkArgumentNonnegative(insets.top);
Preconditions.checkArgumentNonnegative(insets.right);
Preconditions.checkArgumentNonnegative(insets.bottom);
this.insets = insets;
this.textColor = textColor;
this.backgroundColor = backgroundColor;
this.strokeColor = strokeColor;
this.strokeWidth = strokeWidth;
this.itemShape = itemShape;
}
private static DetailedChipView newInstance(Builder builder) {
DetailedChipView detailedChipView = new DetailedChipView(builder.context);
// avatar
if(builder.avatarUri != null)
detailedChipView.setAvatarIcon(builder.avatarUri);
else if(builder.avatarDrawable != null)
detailedChipView.setAvatarIcon(builder.avatarDrawable);
else
detailedChipView.setAvatarIcon(mLetterTileProvider.getLetterTile(builder.name));
// background color
if(builder.backgroundColor != null)
detailedChipView.setBackGroundcolor(builder.backgroundColor);
// text color
if(builder.textColor != null)
detailedChipView.setTextColor(builder.textColor);
else if(ColorUtil.isColorDark(detailedChipView.getBackgroundColor()))
detailedChipView.setTextColor(ColorStateList.valueOf(Color.WHITE));
else
detailedChipView.setTextColor(ColorStateList.valueOf(Color.BLACK));
// delete icon color
if(builder.deleteIconColor != null)
detailedChipView.setDeleteIconColor(builder.deleteIconColor);
else if(ColorUtil.isColorDark(detailedChipView.getBackgroundColor()))
detailedChipView.setDeleteIconColor(ColorStateList.valueOf(Color.WHITE));
else
detailedChipView.setDeleteIconColor(ColorStateList.valueOf(Color.BLACK));
detailedChipView.setName(builder.name);
detailedChipView.setInfo(builder.info);
return detailedChipView;
}
private ColorStateList createRippleState () {
int[][] states = new int[][] { new int[] { android.R.attr.state_enabled} };
int enabled_color = defaultColorStateList.getColorForState(view.getDrawableState(), android.R.attr.state_enabled);
int[] colors = new int[] { Color.argb(70, Color.red(enabled_color), Color.green(enabled_color),
Color.blue(enabled_color)) };
return new ColorStateList(states, colors);
}
/**
* Returns the background color of tabs.
*
* @param tab
* The tab, the background color should be returned for, as an instance of the class
* {@link Tab} or null, if the background color should not be returned for a specific
* tab
* @return The background color of tabs as an instance of the class {@link ColorStateList}
*/
public final ColorStateList getTabBackgroundColor(@Nullable final Tab tab) {
ColorStateList colorStateList = tab != null ? tab.getBackgroundColor() : null;
if (colorStateList == null) {
colorStateList = model.getTabBackgroundColor();
if (colorStateList == null) {
colorStateList = themeHelper.getColorStateList(tabSwitcher.getLayout(),
R.attr.tabSwitcherTabBackgroundColor);
}
}
return colorStateList;
}
public static ColorStateList getColorStateList(int colorStateId, Theme theme) {
Resources resources = NoHttp.getContext().getResources();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
return resources.getColorStateList(colorStateId, theme);
else
return resources.getColorStateList(colorStateId);
}
/**
* Sets color state list as background for this button.
* <p/>
* Xml attribute: {@code app:floatingActionButtonColor}
*
* @param colorStateList color
*/
public void setColorStateList(ColorStateList colorStateList) {
boolean changed = mColorStateList != colorStateList;
mColorStateList = colorStateList;
if (changed) {
updateBackground();
}
}
public static RippleDrawable makeFor(View target, ColorStateList colors, boolean parentIsScrollContainer){
RippleDrawable drawable = new RippleDrawable(colors, target.getBackground());
TouchTracker tracker = new TouchTracker();
tracker.setInsideScrollContainer(parentIsScrollContainer);
ViewUtils.setBackground(target, drawable);
target.setOnTouchListener(tracker);
return drawable;
}
private void setupFabs() {
if(TRUE.equalsIgnoreCase(postItem.getLikes())){
upFab.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.material_green700)));
}else if(FALSE.equalsIgnoreCase(postItem.getLikes())){
downFab.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.material_red700)));
}else{
upFab.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.material_bluegrey900)));
downFab.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.material_bluegrey900)));
}
}
@Override
public void setIconTintList(ColorStateList tintList) {
mExpandDrawable = DrawableCompat.wrap(mExpandDrawable
.getConstantState().newDrawable()).mutate();
DrawableCompat.setTintList(mExpandDrawable, tintList);
mCollapseDrawable = DrawableCompat.wrap(mCollapseDrawable
.getConstantState().newDrawable()).mutate();
DrawableCompat.setTintList(mCollapseDrawable, tintList);
super.setIconTintList(tintList);
}
/**
* Adapts the color of a button, which allows to add a new tab.
*
* @param addTabItem
* The add tab item, which corresponds to the button, whose color should be adapted, as
* an instance of the class {@link AddTabItem}. The add tab item may not be null
*/
private void adaptAddTabButtonColor(@NonNull final AddTabItem addTabItem) {
ColorStateList colorStateList = getStyle().getAddTabButtonColor();
int[] stateSet = new int[]{};
int color = colorStateList.getColorForState(stateSet, colorStateList.getDefaultColor());
View view = addTabItem.getView();
Drawable background = view.getBackground();
background.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
}
/**
* Checks that the {@link MaterialRadioButton} buttonTint matches {@link
* R.color#radiobutton_themeable_attribute_color}.
*/
static void testThemeableButtonTint(RadioButton radioButton) {
ColorStateList buttonTintList = CompoundButtonCompat.getButtonTintList(radioButton);
assertThat(buttonTintList.getColorForState(STATE_CHECKED, Color.BLACK))
.isEqualTo(MaterialColors.getColor(radioButton, R.attr.colorControlActivated));
assertThat(buttonTintList.getColorForState(STATE_UNCHECKED, Color.BLACK))
.isEqualTo(MaterialColors.getColor(radioButton, R.attr.colorOnSurface));
}
public ColorStateList build(){
SpeedyArrayMap<int[], Integer> speedyArrayMap = mColorStateList.copy();
speedyArrayMap.put(mDefaultStateSet,mDefaultStateColor);
int size = speedyArrayMap.size();
int[][] stateSet = new int[size][];
int [] colors = new int[size];
for (int index=0;index<size;index++){
stateSet[index] = speedyArrayMap.keyAt(index);
colors[index] = speedyArrayMap.valueAt(index);
}
return new ColorStateList(stateSet,colors);
}
@Nullable
private PorterDuffColorFilter getTintFilter(@Nullable ColorStateList tint,
@Nullable PorterDuff.Mode tintMode,
int defaultColor) {
return tint == null || tintMode == null ? null : new PorterDuffColorFilter(
tint.getColorForState(getState(), defaultColor), tintMode);
}
/**
* Returns the color of the button, which allows to add a new tab. When using the smartphone
* layout, such a button is never shown. When using the tablet layout, the button is shown next
* to the tabs.
*
* @return The color of the button, which allows to add a new tab, as an instance of the class
* {@link ColorStateList} or null, if the default color is used
*/
public final ColorStateList getAddTabButtonColor() {
ColorStateList colorStateList = model.getAddTabButtonColor();
if (colorStateList == null) {
colorStateList = themeHelper.getColorStateList(tabSwitcher.getLayout(),
R.attr.tabSwitcherAddTabButtonColor);
}
return colorStateList;
}
private void setTextColor(ColorStateList color) {
if (null != mHeaderText) {
mHeaderText.setTextColor(color);
}
if (null != mSubHeaderText) {
mSubHeaderText.setTextColor(color);
}
}
private void setTextColor(ColorStateList color) {
if (null != mHeaderText) {
mHeaderText.setTextColor(color);
}
if (null != mSubHeaderText) {
mSubHeaderText.setTextColor(color);
}
}
public void setTextColor(ColorStateList color) {
mColorStateList = color;
for (int i = 0; i < mLlContainer.getChildCount(); i++) {
TabView tabView = (TabView) mLlContainer.getChildAt(i);
tabView.setTextColor(mColorStateList);
}
if (mColorStateList != null) {
mTvFullScreen.setTextColor(mColorStateList);
}
}
ErrorViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
errorTextView.setText(R.string.load_comments_failed);
errorTextView.setTextColor(mSecondaryTextColor);
retryButton.setOnClickListener(view -> mRetryLoadingMoreCallback.retryLoadingMore());
retryButton.setBackgroundTintList(ColorStateList.valueOf(mColorPrimaryLightTheme));
retryButton.setTextColor(mButtonTextColor);
}
@Override
public void onBindViewHolder(@NonNull MenuItemsAdapter.ViewHolder holder, int position) {
final BaseDrawerItem<E> item = items.get(position);
holder.icon.setImageResource(item.icon);
holder.name.setText(item.name);
if (item.badgeNumber >= 0) {
holder.badge.setVisibility(View.VISIBLE);
holder.badge.setText(String.valueOf(item.badgeNumber));
} else {
holder.badge.setVisibility(View.GONE);
}
holder.itemView.setOnClickListener(v -> {
if (listener != null) listener.onMenuItemSelected(item);
});
if (item.active) {
holder.name.setTextColor(colorAccent);
holder.itemView.setBackgroundResource(R.drawable.item_drawer_active);
holder.icon.setImageTintList(ColorStateList.valueOf(colorAccent));
} else {
holder.name.setTextColor(colorTextPrimary);
holder.icon.setImageTintList(ColorStateList.valueOf(colorTextPrimary));
CommonUtils.setBackground(holder.itemView, android.R.attr.selectableItemBackground);
}
if (width > 0) {
holder.itemView.getLayoutParams().width = width;
holder.itemView.requestLayout();
}
}
private Drawable getMessageSelector(@ColorInt int normalColor, @ColorInt int selectedColor, @ColorInt int pressedColor, @DrawableRes int shape) {
Drawable drawable = DrawableCompat.wrap(getVectorDrawable(shape)).mutate();
DrawableCompat.setTintList(
drawable,
new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_selected},
new int[]{android.R.attr.state_pressed},
new int[]{-android.R.attr.state_pressed, -android.R.attr.state_selected}
},
new int[]{selectedColor, pressedColor, normalColor}
));
return drawable;
}
public static void setItemIconColors(@NonNull NavigationView navigationView, @ColorInt int normalColor, @ColorInt int selectedColor) {
final ColorStateList iconSl = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
},
new int[]{
normalColor,
selectedColor
});
navigationView.setItemIconTintList(iconSl);
}
@Override
public void setIconTintList(ColorStateList tintList) {
mExpandDrawable = DrawableCompat.wrap(mExpandDrawable
.getConstantState().newDrawable()).mutate();
DrawableCompat.setTintList(mExpandDrawable, tintList);
mCollapseDrawable = DrawableCompat.wrap(mCollapseDrawable
.getConstantState().newDrawable()).mutate();
DrawableCompat.setTintList(mCollapseDrawable, tintList);
super.setIconTintList(tintList);
}
public void setBorderColor(ColorStateList colors) {
if (mBorderColor.equals(colors)) { return; }
mBorderColor =
(colors != null) ? colors : ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
updateDrawableAttrs();
updateBackgroundDrawableAttrs(false);
if (mBorderWidth > 0) {
invalidate();
}
}
@Override
public Object[] setup(@NonNull View view, @NonNull TypedArray a, int attr) {
TabLayout tabLayout = (TabLayout) view;
ColorStateList csl1 = tabLayout.getTabTextColors();
ColorStateList csl2 = a.getColorStateList(attr);
return new ColorStateList[]{ csl1, csl2 };
}
/**
* Sets the text color for the layout.
*
* @param colorStateList The text color state list for the layout
* @return This {@link TextLayoutBuilder} instance
*/
public TextLayoutBuilder setTextColor(ColorStateList colorStateList) {
mParams.createNewPaintIfNeeded();
mParams.color = colorStateList;
mParams.paint.setColor(mParams.color != null ? mParams.color.getDefaultColor() : Color.BLACK);
mSavedLayout = null;
return this;
}