下面列出了怎么用android.support.v4.widget.CompoundButtonCompat的API类实例代码及写法,或者点击链接到github查看源代码。
public boolean applySupportButtonDrawableTint() {
Drawable buttonDrawable = CompoundButtonCompat.getButtonDrawable((CompoundButton) mView);
if (buttonDrawable != null && mCompoundButtonTintInfo != null && mCompoundButtonTintInfo.mHasTintList) {
buttonDrawable = DrawableCompat.wrap(buttonDrawable);
buttonDrawable = buttonDrawable.mutate();
if (mCompoundButtonTintInfo.mHasTintList) {
DrawableCompat.setTintList(buttonDrawable, mCompoundButtonTintInfo.mTintList);
}
if (mCompoundButtonTintInfo.mHasTintMode) {
DrawableCompat.setTintMode(buttonDrawable, mCompoundButtonTintInfo.mTintMode);
}
// The drawable (or one of its children) may not have been
// stateful before applying the tint, so let's try again.
if (buttonDrawable.isStateful()) {
buttonDrawable.setState(mView.getDrawableState());
}
setButtonDrawable(buttonDrawable);
return true;
}
return false;
}
public boolean applySupportButtonDrawableTint() {
Drawable buttonDrawable = CompoundButtonCompat.getButtonDrawable(mView);
if (buttonDrawable != null && mCompoundButtonTintInfo != null && mCompoundButtonTintInfo.mHasTintList) {
buttonDrawable = DrawableCompat.wrap(buttonDrawable);
buttonDrawable = buttonDrawable.mutate();
if (mCompoundButtonTintInfo.mHasTintList) {
DrawableCompat.setTintList(buttonDrawable, mCompoundButtonTintInfo.mTintList);
}
if (mCompoundButtonTintInfo.mHasTintMode) {
DrawableCompat.setTintMode(buttonDrawable, mCompoundButtonTintInfo.mTintMode);
}
// The drawable (or one of its children) may not have been
// stateful before applying the tint, so let's try again.
if (buttonDrawable.isStateful()) {
buttonDrawable.setState(mView.getDrawableState());
}
setButtonDrawable(buttonDrawable);
return true;
}
return false;
}
/**
* Compound buttons (ex. {{@link android.widget.CheckBox}}, {@link android.widget.ToggleButton})
* have a button drawable with minimum height and width specified for them.
* To align the behavior with CSS Flexbox we want to respect these minimum measurement to avoid
* these drawables from being cut off during calculation. When the compound button has a minimum
* width or height already specified we will not make any change since we assume those were
* voluntarily set by the user.
*
* @param compoundButton the compound button that need to be evaluated
*/
private void evaluateMinimumSizeForCompoundButton(CompoundButton compoundButton) {
FlexItem flexItem = (FlexItem) compoundButton.getLayoutParams();
int minWidth = flexItem.getMinWidth();
int minHeight = flexItem.getMinHeight();
Drawable drawable = CompoundButtonCompat.getButtonDrawable(compoundButton);
int drawableMinWidth = drawable == null ? 0 : drawable.getMinimumWidth();
int drawableMinHeight = drawable == null ? 0 : drawable.getMinimumHeight();
flexItem.setMinWidth(minWidth == NOT_SET ? drawableMinWidth : minWidth);
flexItem.setMinHeight(minHeight == NOT_SET ? drawableMinHeight : minHeight);
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (ThemeUtils.isSkipAnimatedSelector()) {
Drawable drawable = CompoundButtonCompat.getButtonDrawable(this);
try {
if (ThemeUtils.getWrapperDrawable(drawable) instanceof AnimatedStateListDrawable) {
drawable.jumpToCurrentState();
}
} catch (NoClassDefFoundError error) {
error.printStackTrace();
}
}
}
public int getCompoundPaddingLeft(int superValue) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
// Before JB-MR1 the button drawable wasn't taken into account for padding. We'll
// workaround that here
Drawable buttonDrawable = CompoundButtonCompat.getButtonDrawable((CompoundButton) mView);
if (buttonDrawable != null) {
superValue += buttonDrawable.getIntrinsicWidth();
}
}
return superValue;
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (ThemeUtils.isSkipAnimatedSelector()) {
Drawable drawable = CompoundButtonCompat.getButtonDrawable(this);
try {
if (ThemeUtils.getWrapperDrawable(drawable) instanceof AnimatedStateListDrawable) {
drawable.jumpToCurrentState();
}
} catch (NoClassDefFoundError error) {
error.printStackTrace();
}
}
}
@Override
public void applySkin() {
mButtonResourceId = SkinCompatHelper.checkResourceId(mButtonResourceId);
if (mButtonResourceId != INVALID_ID) {
mView.setButtonDrawable(SkinCompatResources.getDrawableCompat(mView.getContext(), mButtonResourceId));
}
mButtonTintResId = SkinCompatHelper.checkResourceId(mButtonTintResId);
if (mButtonTintResId != INVALID_ID) {
CompoundButtonCompat.setButtonTintList(mView, SkinCompatResources.getColorStateList(mView.getContext(), mButtonTintResId));
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (ThemeUtils.isSkipAnimatedSelector()) {
Drawable drawable = CompoundButtonCompat.getButtonDrawable(this);
try {
if (ThemeUtils.getWrapperDrawable(drawable) instanceof AnimatedStateListDrawable) {
drawable.jumpToCurrentState();
}
} catch (NoClassDefFoundError error) {
error.printStackTrace();
}
}
}
public int getCompoundPaddingLeft(int superValue) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
// Before JB-MR1 the button drawable wasn't taken into account for padding. We'll
// workaround that here
Drawable buttonDrawable = CompoundButtonCompat.getButtonDrawable(mView);
if (buttonDrawable != null) {
superValue += buttonDrawable.getIntrinsicWidth();
}
}
return superValue;
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (ThemeUtils.isSkipAnimatedSelector()) {
Drawable drawable = CompoundButtonCompat.getButtonDrawable(this);
try {
if (ThemeUtils.getWrapperDrawable(drawable) instanceof AnimatedStateListDrawable) {
drawable.jumpToCurrentState();
}
} catch (NoClassDefFoundError error) {
error.printStackTrace();
}
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (ThemeUtils.isSkipAnimatedSelector()) {
Drawable drawable = CompoundButtonCompat.getButtonDrawable(this);
try {
if (ThemeUtils.getWrapperDrawable(drawable) instanceof AnimatedStateListDrawable) {
drawable.jumpToCurrentState();
}
} catch (NoClassDefFoundError error) {
error.printStackTrace();
}
}
}
@Override
protected void setAttr(final CompoundButton view, final ColorStateList colorStateList) {
CompoundButtonCompat.setButtonTintList(view, colorStateList);
}
/**
*
*/
protected void initViews( final Context context, View dialogContent )
{
SuntimesUtils.initDisplayStrings(context);
if (Build.VERSION.SDK_INT >= 14)
{
switchRepeat = (SwitchCompat) dialogContent.findViewById(R.id.alarmOption_repeat);
if (switchRepeat != null)
{
switchRepeat.setOnCheckedChangeListener(onRepeatChanged);
if (colorOverrides[0] != -1) {
switchRepeat.setThumbTintList(SuntimesUtils.colorStateList(
colorOverrides[0],
colorOverrides[1],
colorOverrides[2],
colorOverrides[3]));
switchRepeat.setTrackTintList(SuntimesUtils.colorStateList(
ColorUtils.setAlphaComponent(colorOverrides[0], 85),
ColorUtils.setAlphaComponent(colorOverrides[1], 85),
ColorUtils.setAlphaComponent(colorOverrides[2], 85),
ColorUtils.setAlphaComponent(colorOverrides[3], 85))); // 33% alpha (85 / 255)
}
}
} else {
checkRepeat = (CheckBox) dialogContent.findViewById(R.id.alarmOption_repeat);
if (checkRepeat != null) {
checkRepeat.setOnCheckedChangeListener(onRepeatChanged);
CompoundButtonCompat.setButtonTintList(checkRepeat, SuntimesUtils.colorStateList(colorOverrides[0], colorOverrides[1], colorOverrides[2], colorOverrides[3]));
}
}
btnDays = new SparseArray<>();
btnDays.put(Calendar.SUNDAY, (ToggleButton)dialogContent.findViewById(R.id.alarmOption_repeat_sun));
btnDays.put(Calendar.MONDAY, (ToggleButton)dialogContent.findViewById(R.id.alarmOption_repeat_mon));
btnDays.put(Calendar.TUESDAY, (ToggleButton)dialogContent.findViewById(R.id.alarmOption_repeat_tue));
btnDays.put(Calendar.WEDNESDAY, (ToggleButton)dialogContent.findViewById(R.id.alarmOption_repeat_wed));
btnDays.put(Calendar.THURSDAY, (ToggleButton)dialogContent.findViewById(R.id.alarmOption_repeat_thu));
btnDays.put(Calendar.FRIDAY, (ToggleButton)dialogContent.findViewById(R.id.alarmOption_repeat_fri));
btnDays.put(Calendar.SATURDAY, (ToggleButton)dialogContent.findViewById(R.id.alarmOption_repeat_sat));
int n = btnDays.size();
for (int i=0; i<n; i++)
{
int day = btnDays.keyAt(i);
ToggleButton button = btnDays.get(day);
if (button != null)
{
button.setOnCheckedChangeListener(onRepeatDayChanged);
String dayName = utils.getShortDayString(context, day);
button.setTextOn(dayName);
button.setTextOff(dayName);
}
}
}