下面列出了android.support.v4.view.ViewCompat#setBackgroundTintList ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** Setting up the right button */
private void setupRightButton(FloatingActionButton rightButton, Drawable rightSrc,
int rightButtonTint, int rightDrawableTint) {
if(rightSrc!=null) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
rightSrc.setTintList(new ColorStateList(new int[][]{new int[]{0}},
new int[]{rightDrawableTint}));
}
else
{
final Drawable wrappedDrawable = DrawableCompat.wrap(rightSrc);
DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(rightDrawableTint));
}
rightButton.setImageDrawable(rightSrc);
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
rightButton.setBackgroundTintList(new ColorStateList(new int[][]{new int[]{0}},
new int[]{rightButtonTint}));
}
else
{
ViewCompat.setBackgroundTintList(rightButton, ColorStateList.valueOf(rightButtonTint));
}
}
/** Setting up the left button */
private void setupLeftButton(FloatingActionButton leftButton, Drawable leftSrc,
int leftButtonTint, int leftDrawableTint) {
if(leftSrc!=null) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
leftSrc.setTintList(new ColorStateList(new int[][]{new int[]{0}},
new int[]{leftDrawableTint}));
}
else
{
final Drawable wrappedDrawable = DrawableCompat.wrap(leftSrc);
DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(leftDrawableTint));
}
leftButton.setImageDrawable(leftSrc);
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
leftButton.setBackgroundTintList(new ColorStateList(new int[][]{new int[]{0}},
new int[]{leftButtonTint}));
}
else
{
ViewCompat.setBackgroundTintList(leftButton, ColorStateList.valueOf(leftButtonTint));
}
}
private void setupRightButton(ImageButton rightButton, Drawable rightSrc,
int rightButtonTint, int rightDrawableTint, Drawable background) {
if(rightSrc!=null) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
rightSrc.setTintList(new ColorStateList(new int[][]{new int[]{0}},
new int[]{rightDrawableTint}));
}
else
{
final Drawable wrappedDrawable = DrawableCompat.wrap(rightSrc);
DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(rightDrawableTint));
}
rightButton.setImageDrawable(rightSrc);
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
rightButton.setBackgroundTintList(new ColorStateList(new int[][]{new int[]{0}},
new int[]{rightButtonTint}));
}
else
{
ViewCompat.setBackgroundTintList(rightButton, ColorStateList.valueOf(rightButtonTint));
}
rightButton.setBackground(background);
}
private void setupLeftButton(ImageButton leftButton, Drawable leftSrc,
int leftButtonTint, int leftDrawableTint,Drawable background) {
if(leftSrc!=null) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
leftSrc.setTintList(new ColorStateList(new int[][]{new int[]{0}},
new int[]{leftDrawableTint}));
}
else
{
final Drawable wrappedDrawable = DrawableCompat.wrap(leftSrc);
DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(leftDrawableTint));
}
leftButton.setImageDrawable(leftSrc);
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
leftButton.setBackgroundTintList(new ColorStateList(new int[][]{new int[]{0}},
new int[]{leftButtonTint}));
}
else
{
ViewCompat.setBackgroundTintList(leftButton, ColorStateList.valueOf(leftButtonTint));
}
leftButton.setBackground(background);
}
private void init() {
mValidationCallback = new ValidationCallback() {
@Override
public void execute(ValidationHolder validationHolder, Matcher matcher) {
TextView textView = replaceView(validationHolder);
if (AwesomeValidation.isAutoFocusOnFirstFailureEnabled() && !mHasFailed) {
textView.setFocusable(true);
textView.setFocusableInTouchMode(true);
textView.setClickable(true);
textView.requestFocus();
mHasFailed = true;
}
ViewCompat.setBackgroundTintList(validationHolder.getEditText(), ColorStateList.valueOf(mColor));
}
};
}
/**
* Tint the button
*
* @param button the button
* @param color the color
*/
public static void tint(@NonNull Button button, @ColorInt int color) {
ColorStateList sl = new ColorStateList(new int[][]{
new int[]{}
}, new int[]{
color
});
ViewCompat.setBackgroundTintList(button, sl);
}
/**
*
* @param view
* @param attrs
* @param defStyleAttr
*/
public static void loadFromAttributes(View view, AttributeSet attrs, int defStyleAttr) {
TypedArray a = view.getContext().obtainStyledAttributes(attrs, R.styleable.EmBackgroundTintHelper, defStyleAttr, 0);
try {
if (a.hasValue(R.styleable.EmBackgroundTintHelper_backgroundTint)) {
ViewCompat.setBackgroundTintList(view, a.getColorStateList(R.styleable.EmBackgroundTintHelper_backgroundTint));
}
if (a.hasValue(R.styleable.EmBackgroundTintHelper_backgroundTintMode)) {
ViewCompat.setBackgroundTintMode(view, DrawableUtils.parseTintMode( a.getInt(R.styleable.EmBackgroundTintHelper_backgroundTintMode, -1), null));
}
} finally {
a.recycle();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
mPresenter = new LoginPresenterImpl(this, this);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && loginFacebookButton instanceof AppCompatButton) {
((AppCompatButton) loginFacebookButton).setSupportBackgroundTintList(getResources().getColorStateList(R.color.facebook_blue));
} else {
ViewCompat.setBackgroundTintList(loginFacebookButton, getResources().getColorStateList(R.color.facebook_blue));
}
}
/**
* display Material Design OverlayView
*/
public void setOverlayStyle_MaterialDesign(int color) {
if (mMDOverlay == null) {
initMDOverlay(color);
} else {
ViewCompat.setBackgroundTintList(mMDOverlay, ColorStateList.valueOf(color));
}
mCenterOverlay = null;
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
overflowGestureListener.clearReferences();
ViewCompat.setBackgroundTintList(fab, null);
fab.setImageResource(0);
fab.setImageDrawable(null);
fab.setImageBitmap(null);
fab = null;
contentFrame = null;
}
public void restoreViews() {
if (!mIsOriginal) {
ViewCompat.setBackgroundTintList(mEditText, mColorStateList);
mEditText.requestFocus();
mNewContainer.removeView(mEditText);
mParent.removeView(mNewContainer);
mParent.addView(mEditText, mIndex);
mIsOriginal = true;
}
}
public void testRestoreViews() {
mViewsInfo.restoreViews();
ViewCompat.setBackgroundTintList(mMockEditText, mMockColorStateList);
InOrder order = inOrder(mMockEditText, mMockNewContainer, mMockParent);
order.verify(mMockEditText).requestFocus();
order.verify(mMockNewContainer).removeView(mMockEditText);
order.verify(mMockParent).removeView(mMockNewContainer);
order.verify(mMockParent).addView(mMockEditText, mIndex);
}
@Override
protected void setAttr(final View view, final int color) {
ViewCompat.setBackgroundTintList(view, ColorStateList.valueOf(color));
}
private void tintButtons(ColorStateList color) {
ViewCompat.setBackgroundTintList(nextButton, color);
ViewCompat.setBackgroundTintList(backButton, color);
ViewCompat.setBackgroundTintList(skipButton, color);
}
private void tintButtons(ColorStateList color) {
ViewCompat.setBackgroundTintList(nextButton, color);
ViewCompat.setBackgroundTintList(backButton, color);
ViewCompat.setBackgroundTintList(skipButton, color);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle arguments = getArguments();
View fragment = inflater.inflate(arguments.getInt(ARGUMENT_LAYOUT_RES,
R.layout.fragment_simple_slide), container, false);
TextView titleView = (TextView) fragment.findViewById(R.id.mi_title);
TextView descriptionView = (TextView) fragment.findViewById(R.id.mi_description);
buttonGrantPermissions = (Button) fragment.findViewById(R.id.mi_button_grant_permissions);
ImageView imageView = (ImageView) fragment.findViewById(R.id.mi_image);
CharSequence title = arguments.getCharSequence(ARGUMENT_TITLE);
int titleRes = arguments.getInt(ARGUMENT_TITLE_RES);
CharSequence description = arguments.getCharSequence(ARGUMENT_DESCRIPTION);
int descriptionRes = arguments.getInt(ARGUMENT_DESCRIPTION_RES);
int imageRes = arguments.getInt(ARGUMENT_IMAGE_RES);
int backgroundRes = arguments.getInt(ARGUMENT_BACKGROUND_RES);
int backgroundDarkRes = arguments.getInt(ARGUMENT_BACKGROUND_DARK_RES);
//Title
if (titleView != null) {
if (title != null) {
titleView.setText(title);
titleView.setVisibility(View.VISIBLE);
} else if (titleRes != 0) {
titleView.setText(titleRes);
titleView.setVisibility(View.VISIBLE);
} else {
titleView.setVisibility(View.GONE);
}
}
//Description
if (descriptionView != null) {
if (description != null) {
descriptionView.setText(description);
descriptionView.setVisibility(View.VISIBLE);
} else if (descriptionRes != 0) {
descriptionView.setText(descriptionRes);
descriptionView.setVisibility(View.VISIBLE);
} else {
descriptionView.setVisibility(View.GONE);
}
}
//Image
if (imageView != null) {
if (imageRes != 0) {
imageView.setImageResource(imageRes);
imageView.setVisibility(View.VISIBLE);
} else {
imageView.setVisibility(View.GONE);
}
}
if (backgroundDarkRes != 0 && buttonGrantPermissions != null) {
ViewCompat.setBackgroundTintList(buttonGrantPermissions, ColorStateList.valueOf(
ContextCompat.getColor(getContext(), backgroundDarkRes)));
}
@ColorInt
int textColorPrimary;
@ColorInt
int textColorSecondary;
if (backgroundRes != 0 &&
ColorUtils.calculateLuminance(ContextCompat.getColor(getContext(), backgroundRes)) < 0.6) {
//Use light text color
textColorPrimary = ContextCompat.getColor(getContext(), R.color.mi_text_color_primary_dark);
textColorSecondary = ContextCompat.getColor(getContext(), R.color.mi_text_color_secondary_dark);
} else {
//Use dark text color
textColorPrimary = ContextCompat.getColor(getContext(), R.color.mi_text_color_primary_light);
textColorSecondary = ContextCompat.getColor(getContext(), R.color.mi_text_color_secondary_light);
}
if (titleView != null) {
titleView.setTextColor(textColorPrimary);
}
if (descriptionView != null) {
descriptionView.setTextColor(textColorSecondary);
}
if (buttonGrantPermissions != null) {
buttonGrantPermissions.setTextColor(textColorPrimary);
}
return fragment;
}
public static void resetTintColor(Context context, View view) {
TintManager tintManager = TintManager.get(context);
ViewCompat.setBackgroundTintList(view,
tintManager.getTintList(android.support.design.R.drawable.abc_edit_text_material));
}
private FloatingActionButton createFab(Context context, int buttonDrawable, int buttonColor, int buttonPosition) {
fab = new TintFloatingActionButton(context);
int fabElevationInPixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
LayoutParams fabLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
fabMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics());
if (buttonPosition == RIGHT) {
fabLayoutParams.gravity = Gravity.END | Gravity.TOP;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
fabLayoutParams.setMarginEnd(fabMargin);
} else {
fabLayoutParams.rightMargin = fabMargin;
}
} else if (buttonPosition == CENTER) {
fabLayoutParams.gravity = Gravity.CENTER | Gravity.TOP;
} else {
fabLayoutParams.gravity = Gravity.START | Gravity.TOP;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
fabLayoutParams.setMarginStart(fabMargin);
} else {
fabLayoutParams.leftMargin = fabMargin;
}
}
fabLayoutParams.bottomMargin = fabMargin;
fabLayoutParams.topMargin = fabMargin;
if (buttonDrawable > 0) {
fab.setImageDrawable(ContextCompat.getDrawable(context, buttonDrawable));
}
if (buttonColor > 0) {
ViewCompat.setBackgroundTintList(fab, ContextCompat.getColorStateList(context, buttonColor));
}
ViewCompat.setElevation(fab, fabElevationInPixels);
fab.setLayoutParams(fabLayoutParams);
fab.setTag("FAB");
this.addView(fab);
return fab;
}