下面列出了android.util.AttributeSet#getAttributeBooleanValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Extracts state_ attributes from an attribute set.
*
* @param attrs The attribute set.
*
* @return An array of state_ attributes.
*/
static int[] extractStateSet(AttributeSet attrs) {
int j = 0;
final int numAttrs = attrs.getAttributeCount();
int[] states = new int[numAttrs];
for (int i = 0; i < numAttrs; i++) {
final int stateResId = attrs.getAttributeNameResource(i);
if (stateResId == 0) {
break;
} else if (stateResId == android.R.attr.drawable || stateResId == android.R.attr.id || stateResId == R.attr.drawableTint || stateResId == R.attr.drawableTintMode) {
// Ignore attributes from StateListDrawableItem and
// AnimatedStateListDrawableItem.
continue;
} else {
states[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId;
}
}
states = StateSet.trimStateSet(states, j);
return states;
}
protected static int[] extractStateSet(AttributeSet attrs) {
int j = 0;
final int numAttrs = attrs.getAttributeCount();
int[] states = new int[numAttrs];
for (int i = 0; i < numAttrs; i++) {
final int stateResId = attrs.getAttributeNameResource(i);
switch (stateResId) {
case 0:
break;
case android.R.attr.color:
case android.R.attr.alpha:
// Ignore attributes from StateListDrawableItem and
// AnimatedStateListDrawableItem.
continue;
default:
states[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId;
}
}
states = StateSet.trimStateSet(states, j);
return states;
}
private void init(Context context, AttributeSet attrs) {
mDensity = getContext().getResources().getDisplayMetrics().density;
setOnPreferenceClickListener(this);
if (attrs != null) {
mAlphaSliderEnabled = attrs.getAttributeBooleanValue(null, "alphaSlider", false);
mHexValueEnabled = attrs.getAttributeBooleanValue(null, "hexValue", false);
}
}
private void validateAttributes(@Nullable AttributeSet attributeSet) {
if (attributeSet == null) {
return;
}
if (attributeSet.getAttributeValue(NAMESPACE_ANDROID, "background") != null) {
Log.w(TAG, "Do not set the background; Chip manages its own background drawable.");
}
if (attributeSet.getAttributeValue(NAMESPACE_ANDROID, "drawableLeft") != null) {
throw new UnsupportedOperationException("Please set left drawable using R.attr#chipIcon.");
}
if (attributeSet.getAttributeValue(NAMESPACE_ANDROID, "drawableStart") != null) {
throw new UnsupportedOperationException("Please set start drawable using R.attr#chipIcon.");
}
if (attributeSet.getAttributeValue(NAMESPACE_ANDROID, "drawableEnd") != null) {
throw new UnsupportedOperationException("Please set end drawable using R.attr#closeIcon.");
}
if (attributeSet.getAttributeValue(NAMESPACE_ANDROID, "drawableRight") != null) {
throw new UnsupportedOperationException("Please set end drawable using R.attr#closeIcon.");
}
if (!attributeSet.getAttributeBooleanValue(NAMESPACE_ANDROID, "singleLine", true)
|| (attributeSet.getAttributeIntValue(NAMESPACE_ANDROID, "lines", 1) != 1)
|| (attributeSet.getAttributeIntValue(NAMESPACE_ANDROID, "minLines", 1) != 1)
|| (attributeSet.getAttributeIntValue(NAMESPACE_ANDROID, "maxLines", 1) != 1)) {
throw new UnsupportedOperationException("Chip does not support multi-line text");
}
if (attributeSet.getAttributeIntValue(
NAMESPACE_ANDROID, "gravity", (Gravity.CENTER_VERTICAL | Gravity.START))
!= (Gravity.CENTER_VERTICAL | Gravity.START)) {
Log.w(TAG, "Chip text must be vertically center and start aligned");
}
}
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
// 获取是否应用换肤操作
boolean hasApplySkin = attrs.getAttributeBooleanValue(NAME_PLACE, SKIN_FLAG, false);
View view = appCompatActivity.getDelegate().createView(parent, name, context, attrs);
if (view == null) {
view = ViewProducer.createViewFromTag(context, name, attrs);
}
if (hasApplySkin) {
return applySkin(context, view, attrs);
}
return view;
}
@Override
protected void setAttributes(AttributeSet attrs) {
super.setAttributes(attrs);
if (!isInEditMode()) {
getBackground().setAlpha(0);
}
iSchecked = attrs.getAttributeBooleanValue(MATERIALDESIGNXML, "checked", false);
eventCheck = iSchecked;
//添加监听器,如果点击了这个控件(不包括ball的区域),这个控件就开始判断是否是开启状态。
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
setChecked(iSchecked ? false : true);
}
});
float size = 20;
String thumbSize = attrs.getAttributeValue(MATERIALDESIGNXML, "thumbSize");
if (thumbSize != null) {
size = Utils.dipOrDpToFloat(thumbSize);
}
ball = new Ball(getContext());
setThumbParams(size);
addView(ball);
// 给圆球添加监听器,点击圆球后就开始判断是否进入开启状态
ball.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
setChecked(iSchecked ? false : true);
}
});
}
protected void setRippleAttributes(AttributeSet attrs) {
/**
* 初始化按压时涟漪的颜色
* Set Ripple Color
* Color by resource
*/
int color = attrs.getAttributeResourceValue(MATERIALDESIGNXML,"rippleColor",-1);
if(color != -1){
rippleColor = getResources().getColor(color);
settedRippleColor = true;
}else{
// Color by hexadecimal
int rColor = attrs.getAttributeIntValue(MATERIALDESIGNXML, "rippleColor", -1);// 16进制的颜色
if(rColor != -1 && !isInEditMode()) {
rippleColor = rColor;
settedRippleColor = true;
}
}
/**
* 初始化涟漪扩展的速度
* init Ripple speed
*/
rippleSpeed = attrs.getAttributeFloatValue(MATERIALDESIGNXML, "rippleSpeed", rippleSpeed);
/**
* 设定涟漪的响应时间
*/
clickAfterRipple = attrs.getAttributeBooleanValue(MATERIALDESIGNXML, "clickAfterRipple", clickAfterRipple);
}
public Button(Context context, AttributeSet attrs) {
super(context, attrs);
setDefaultProperties();
clickAfterRipple = attrs.getAttributeBooleanValue(MATERIALDESIGNXML,
"animate", true);
setAttributes(attrs);
beforeBackground = backgroundColor;
if (rippleColor == null)
rippleColor = makePressColor();
}
protected void setAttributes(AttributeSet attrs) {
setBackgroundResource(R.drawable.background_transparent);
// Set size of view
setMinimumHeight(Utils.dpToPx(48, getResources()));
setMinimumWidth(Utils.dpToPx(80, getResources()));
// Set background Color
// Color by resource
int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,
"background", -1);
if (bacgroundColor != -1) {
setBackgroundColor(getResources().getColor(bacgroundColor));
} else {
// Color by hexadecimal
int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
if (background != -1)
setBackgroundColor(background);
}
check = attrs.getAttributeBooleanValue(MATERIALDESIGNXML, "check",
false);
eventCheck = check;
ball = new Ball(getContext());
RelativeLayout.LayoutParams params = new LayoutParams(Utils.dpToPx(20,
getResources()), Utils.dpToPx(20, getResources()));
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
ball.setLayoutParams(params);
addView(ball);
}
private void init(Context context, AttributeSet attrs) {
mDensity = getContext().getResources().getDisplayMetrics().density;
setOnPreferenceClickListener(this);
if (attrs != null) {
mAlphaSliderEnabled = attrs.getAttributeBooleanValue(null, "alphaSlider", false);
mHexValueEnabled = attrs.getAttributeBooleanValue(null, "hexValue", false);
}
}
protected void setRippleAttributes(AttributeSet attrs) {
/**
* 初始化按压时涟漪的颜色
* Set Ripple Color
* Color by resource
*/
int color = attrs.getAttributeResourceValue(MATERIALDESIGNXML,"rippleColor",-1);
if(color != -1){
rippleColor = getResources().getColor(color);
settedRippleColor = true;
}else{
// Color by hexadecimal
int rColor = attrs.getAttributeIntValue(MATERIALDESIGNXML, "rippleColor", -1);// 16进制的颜色
if(rColor != -1 && !isInEditMode()) {
rippleColor = rColor;
settedRippleColor = true;
}
}
/**
* 初始化涟漪扩展的速度
* init Ripple speed
*/
rippleSpeed = attrs.getAttributeFloatValue(MATERIALDESIGNXML, "rippleSpeed", rippleSpeed);
/**
* 设定涟漪的响应时间
*/
clickAfterRipple = attrs.getAttributeBooleanValue(MATERIALDESIGNXML, "clickAfterRipple", clickAfterRipple);
}
protected void setAttributes(AttributeSet attrs) {
setBackgroundResource(R.drawable.background_transparent);
// Set size of view
setMinimumHeight(Utils.dpToPx(48, getResources()));
setMinimumWidth(Utils.dpToPx(80, getResources()));
// Set background Color
// Color by resource
int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,
"background", -1);
if (bacgroundColor != -1) {
setBackgroundColor(getResources().getColor(bacgroundColor));
} else {
// Color by hexadecimal
int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
if (background != -1)
setBackgroundColor(background);
}
check = attrs.getAttributeBooleanValue(MATERIALDESIGNXML, "check",
false);
eventCheck = check;
ball = new Ball(getContext());
RelativeLayout.LayoutParams params = new LayoutParams(Utils.dpToPx(20,
getResources()), Utils.dpToPx(20, getResources()));
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
ball.setLayoutParams(params);
addView(ball);
}
public AppPickerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mResources = mContext.getResources();
mDefaultSummaryText = (String) getSummary();
mAppIconSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40,
mResources.getDisplayMetrics());
mAppIconPreviewSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60,
mResources.getDisplayMetrics());
mIconPickSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50,
mResources.getDisplayMetrics());
mPackageManager = mContext.getPackageManager();
mMode = MODE_APP;
mAppInfo = new AppInfo();
mAllowGravityBoxActions = true;
if (attrs != null) {
mIconPickerEnabled = attrs.getAttributeBooleanValue(null, "iconPickerEnabled", true);
mNullItemEnabled = attrs.getAttributeBooleanValue(null, "nullItemEnabled", true);
mForceCustomIcon = attrs.getAttributeBooleanValue(null, "forceCustomIcon", false);
mAllowGravityBoxActions = attrs.getAttributeBooleanValue(null, "allowGravityBoxActions", true);
}
setDialogLayoutResource(R.layout.app_picker_preference);
setPositiveButtonText(null);
if (sIconPickerAdapter == null) {
initializeIconPickerAdapter();
}
}
public SeekBarPreference(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs != null) {
mMinimum = attrs.getAttributeIntValue(null, "minimum", 0);
mMaximum = attrs.getAttributeIntValue(null, "maximum", 100);
mInterval = attrs.getAttributeIntValue(null, "interval", 1);
mDefaultValue = mMinimum;
mMonitorBoxEnabled = attrs.getAttributeBooleanValue(null, "monitorBoxEnabled", false);
mMonitorBoxUnit = attrs.getAttributeValue(null, "monitorBoxUnit");
}
mHandler = new Handler();
}
public TimePreference(Context ctxt, AttributeSet attrs) {
super(ctxt, attrs);
mDefaultSummaryText = (String) super.getSummary();
if (attrs != null) {
mTimerMode = attrs.getAttributeBooleanValue(null, "timerMode", false);
}
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
}
public Button(Context context, AttributeSet attrs) {
super(context, attrs);
setDefaultProperties();
clickAfterRipple = attrs.getAttributeBooleanValue(MATERIALDESIGNXML,
"animate", true);
setAttributes(attrs);
beforeBackground = backgroundColor;
if (rippleColor == null)
rippleColor = makePressColor();
}
protected void setAttributes(AttributeSet attrs) {
setBackgroundResource(R.drawable.background_checkbox);
// Set size of view
setMinimumHeight(Utils.dpToPx(48, getResources()));
setMinimumWidth(Utils.dpToPx(48, getResources()));
// Set background Color
// Color by resource
int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,
"background", -1);
if (bacgroundColor != -1) {
setBackgroundColor(getResources().getColor(bacgroundColor));
} else {
// Color by hexadecimal
// Color by hexadecimal
int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
if (background != -1)
setBackgroundColor(background);
}
final boolean check = attrs.getAttributeBooleanValue(MATERIALDESIGNXML,
"check", false);
post(new Runnable() {
@Override
public void run() {
setChecked(check);
setPressed(false);
changeBackgroundColor(getResources().getColor(
android.R.color.transparent));
}
});
checkView = new Check(getContext());
checkView.setId(View.generateViewId());
RelativeLayout.LayoutParams params = new LayoutParams(Utils.dpToPx(20,
getResources()), Utils.dpToPx(20, getResources()));
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
checkView.setLayoutParams(params);
addView(checkView);
// Adding text view to checkbox
int textResource = attrs.getAttributeResourceValue(ANDROIDXML, "text", -1);
String text = null;
if(textResource != -1) {
text = getResources().getString(textResource);
} else {
text = attrs.getAttributeValue(ANDROIDXML, "text");
}
if(text != null) {
params.removeRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
TextView textView = new TextView(getContext());
RelativeLayout.LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textViewLayoutParams.addRule(RelativeLayout.RIGHT_OF, checkView.getId());
textViewLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
textViewLayoutParams.setMargins(10, 0, 0, 0);
textView.setLayoutParams(textViewLayoutParams);
textView.setText(text);
addView(textView);
}
}
protected void setAttributes(AttributeSet attrs) {
setBackgroundResource(R.drawable.background_checkbox);
// Set size of view
setMinimumHeight(Utils.dpToPx(48, getResources()));
setMinimumWidth(Utils.dpToPx(48, getResources()));
// Set background Color
// Color by resource
int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,
"background", -1);
if (bacgroundColor != -1) {
setBackgroundColor(getResources().getColor(bacgroundColor));
} else {
// Color by hexadecimal
// Color by hexadecimal
int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
if (background != -1)
setBackgroundColor(background);
}
final boolean check = attrs.getAttributeBooleanValue(MATERIALDESIGNXML,
"check", false);
post(new Runnable() {
@Override
public void run() {
setChecked(check);
setPressed(false);
changeBackgroundColor(getResources().getColor(
android.R.color.transparent));
}
});
checkView = new Check(getContext());
checkView.setId(View.generateViewId());
RelativeLayout.LayoutParams params = new LayoutParams(Utils.dpToPx(20,
getResources()), Utils.dpToPx(20, getResources()));
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
checkView.setLayoutParams(params);
addView(checkView);
// Adding text view to checkbox
int textResource = attrs.getAttributeResourceValue(ANDROIDXML, "text", -1);
String text = null;
if(textResource != -1) {
text = getResources().getString(textResource);
} else {
text = attrs.getAttributeValue(ANDROIDXML, "text");
}
if(text != null) {
params.removeRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
TextView textView = new TextView(getContext());
RelativeLayout.LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textViewLayoutParams.addRule(RelativeLayout.RIGHT_OF, checkView.getId());
textViewLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
textViewLayoutParams.setMargins(10, 0, 0, 0);
textView.setLayoutParams(textViewLayoutParams);
textView.setText(text);
addView(textView);
}
}
protected void setAttributes(AttributeSet attrs) {
setBackgroundResource(R.drawable.background_transparent);
// Set size of view
setMinimumHeight(Utils.dpToPx(48, getResources()));
setMinimumWidth(Utils.dpToPx(80, getResources()));
// Set background Color
// Color by resource
int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,
"background", -1);
if (bacgroundColor != -1) {
setBackgroundColor(getResources().getColor(bacgroundColor));
} else {
// Color by hexadecimal
int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
if (background != -1)
setBackgroundColor(background);
}
showNumberIndicator = attrs.getAttributeBooleanValue(MATERIALDESIGNXML,
"showNumberIndicator", false);
min = attrs.getAttributeIntValue(MATERIALDESIGNXML, "min", 0);
max = attrs.getAttributeIntValue(MATERIALDESIGNXML, "max", 0);
value = attrs.getAttributeIntValue(MATERIALDESIGNXML, "value", min);
ball = new Ball(getContext());
RelativeLayout.LayoutParams params = new LayoutParams(Utils.dpToPx(20,
getResources()), Utils.dpToPx(20, getResources()));
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
ball.setLayoutParams(params);
addView(ball);
// Set if slider content number indicator
// TODO
if (showNumberIndicator) {
numberIndicator = new NumberIndicator(getContext());
}
}
protected void setAttributes(AttributeSet attrs) {
setBackgroundResource(R.drawable.background_transparent);
// Set size of view
setMinimumHeight(Utils.dpToPx(48, getResources()));
setMinimumWidth(Utils.dpToPx(80, getResources()));
// Set background Color
// Color by resource
int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,
"background", -1);
if (bacgroundColor != -1) {
setBackgroundColor(getResources().getColor(bacgroundColor));
} else {
// Color by hexadecimal
int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
if (background != -1)
setBackgroundColor(background);
}
showNumberIndicator = attrs.getAttributeBooleanValue(MATERIALDESIGNXML,
"showNumberIndicator", false);
min = attrs.getAttributeIntValue(MATERIALDESIGNXML, "min", 0);
max = attrs.getAttributeIntValue(MATERIALDESIGNXML, "max", 0);
value = attrs.getAttributeIntValue(MATERIALDESIGNXML, "value", min);
ball = new Ball(getContext());
RelativeLayout.LayoutParams params = new LayoutParams(Utils.dpToPx(20,
getResources()), Utils.dpToPx(20, getResources()));
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
ball.setLayoutParams(params);
addView(ball);
// Set if slider content number indicator
// TODO
if (showNumberIndicator) {
numberIndicator = new NumberIndicator(getContext());
}
}