下面列出了android.util.AttributeSet#getAttributeResourceValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Sets the numColumns based on the attributeset
*/
private void init(AttributeSet attrs) {
// Read numColumns out of the AttributeSet
int count = attrs.getAttributeCount();
if (count > 0) {
for (int i = 0; i < count; i++) {
String name = attrs.getAttributeName(i);
if (name != null && name.equals("numColumns")) {
// Update columns
this.numColumnsID = attrs.getAttributeResourceValue(i, 1);
updateColumns();
break;
}
}
}
Log.d(TAG, "numColumns set to: " + numColumns);
}
protected void setAttributes(AttributeSet attrs){
setBackgroundResource(R.drawable.background_button_rectangle);
//Set background Color
// Color by resource
int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,"background",-1);
if(bacgroundColor != -1){
setBackgroundColor(getResources().getColor(bacgroundColor));
}else{
// Color by hexadecimal
String background = attrs.getAttributeValue(ANDROIDXML,"background");
if(background != null)
setBackgroundColor(Color.parseColor(background));
else
setBackgroundColor(this.backgroundColor);
}
}
/**
* 设置背景色
* Set background Color
*/
public void init(View view, AttributeSet attrs) {
mView = view;
if (attrs == null) {
mEnable = false;
return;
}
mLightBackgroundRes = attrs.getAttributeResourceValue(ANDROIDXML, "background", -1);
mDarkBackgroundRes = attrs.getAttributeResourceValue(MATERIALDESIGNXML, "nightBackground", -1);
if (mLightBackgroundRes == -1) {
mLightBackgroundColor = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
}
if (mDarkBackgroundRes == -1) {
mDarkBackgroundColor = attrs.getAttributeIntValue(MATERIALDESIGNXML, "nightBackground", -1);
}
}
@Override
public void init(View view, AttributeSet attrs) {
super.init(view, attrs);
if (attrs == null) {
mEnable = false;
return;
}
mDividerRes = attrs.getAttributeResourceValue(ANDROIDXML, "divider", -1);
if (mDividerRes == -1) {
mDivider = attrs.getAttributeIntValue(ANDROIDXML, "divider", -1);
}
mNightLVDividerRes = attrs.getAttributeResourceValue(MATERIALDESIGNXML, "nightLVDivider", -1);
if (mNightLVDividerRes != -1) {
mNightLVDivider = attrs.getAttributeIntValue(MATERIALDESIGNXML, "nightLVDivider", -1);
}
}
/**
* Tries to pull the Custom Attribute directly from the TextView.
*
* @param context Activity Context
* @param attrs View Attributes
* @param attributeId if -1 returns null.
* @return null if attribute is not defined or added to View
*/
static String pullFontPathFromView(Context context, AttributeSet attrs, int[] attributeId) {
if (attributeId == null || attrs == null)
return null;
final String attributeName;
try {
attributeName = context.getResources().getResourceEntryName(attributeId[0]);
} catch (Resources.NotFoundException e) {
// invalid attribute ID
return null;
}
final int stringResourceId = attrs.getAttributeResourceValue(null, attributeName, -1);
return stringResourceId > 0
? context.getString(stringResourceId)
: attrs.getAttributeValue(null, attributeName);
}
private void init(Context context, AttributeSet attrs) {
setOnPreferenceClickListener(this);
if (attrs != null) {
int title = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "title", -1);
if (title != -1) {
mTitle = context.getString(title);
}
mBindSummary = attrs.getAttributeBooleanValue(null, "bindSummary", DEFAULT_BIND_SUMMARY);
mMin = attrs.getAttributeIntValue(null, "min", DEFAULT_MIN);
mMax = attrs.getAttributeIntValue(null, "max", DEFAULT_MAX);
mStep = attrs.getAttributeIntValue(null, "step", DEFAULT_STEP);
mCurrentValue = mMin;
}
if (mTitle == null) {
mTitle = "Choose delay (minutes)";
}
if (mMax < mMin) {
throw new AssertionError("max value must be > min value");
}
if (mStep <= 0) {
throw new AssertionError("step value must be > 0");
}
}
private void init(final Context context, final AttributeSet attrs) {
if (AppContext.DEBUG) {
Log.d(ColorPickerPreference.TAG, "init");
}
this.mDensity = getContext().getResources().getDisplayMetrics().density;
setOnPreferenceClickListener(this);
if (attrs != null) {
final String defaultValue = attrs.getAttributeValue(
ColorPickerPreference.ANDROID_NS, "defaultValue");
if (defaultValue.startsWith("#")) {
try {
this.mDefaultValue = ColorPickerPreference
.convertToColorInt(defaultValue);
} catch (final NumberFormatException e) {
this.mDefaultValue = ColorPickerPreference
.convertToColorInt("#FF000000");
}
} else {
final int resourceId = attrs.getAttributeResourceValue(
ColorPickerPreference.ANDROID_NS, "defaultValue", 0);
if (resourceId != 0) {
this.mDefaultValue = context.getResources().getColor(
resourceId);
}
}
this.mAlphaSliderEnabled = attrs.getAttributeBooleanValue(null,
"alphaSlider", false);
}
this.mValue = this.mDefaultValue;
}
@SuppressWarnings("deprecation")
@Override
protected void onInitAttributes(AttributeSet attrs) {
super.onInitAttributes(attrs);
// 设置按钮中的图标
int iconResource = attrs.getAttributeResourceValue(MATERIALDESIGNXML,"iconDrawable",-1);
if (iconResource != -1) {
iconDrawable = getResources().getDrawable(iconResource);
}
// animation
boolean animate = attrs.getAttributeBooleanValue(MATERIALDESIGNXML, "animate", false);
if (animate) {
playAnimation();
}
if (iconDrawable != null) {
icon.setBackgroundDrawable(iconDrawable);
}
// 设置按钮中图标的大小
String size = attrs.getAttributeValue(MATERIALDESIGNXML, "iconSize");
if (size != null) {
iconSize = (int) Utils.dipOrDpToFloat(size);
}
setIconParams();
addView(icon);
}
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);
}
protected void setAttributes(AttributeSet attrs) {
setBackgroundResource(R.drawable.background_transparent);
// Set size of view
setMinimumHeight(dpToPx(48, getResources()));
setMinimumWidth(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);
}
min = attrs.getAttributeIntValue(MATERIALDESIGNXML, "min", 0);
max = attrs.getAttributeIntValue(MATERIALDESIGNXML, "max", 0);
value = attrs.getAttributeIntValue(MATERIALDESIGNXML, "value", min);
ball = new Ball(getContext());
LayoutParams params = new LayoutParams(dpToPx(15, getResources()), dpToPx(15, getResources()));
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
ball.setLayoutParams(params);
addView(ball);
}
public static String getStringAttribute(final Context context, final AttributeSet attrs, final String namespace,
final String name, final String defValue) {
final int resId = attrs.getAttributeResourceValue(namespace, name, Integer.MIN_VALUE);
if (resId != Integer.MIN_VALUE) {
return context.getResources().getString(resId);
}
return LengthUtils.safeString(attrs.getAttributeValue(namespace, name), defValue);
}
private static int getResourceId(ImageView view, AttributeSet attrs, final boolean isSrc) {
final int resId = attrs.getAttributeResourceValue(ANDROID_NS, isSrc ? "src" : "background", 0);
if (resId > 0) {
final String resourceTypeName = view.getResources().getResourceTypeName(resId);
if (SUPPORTED_RESOURCE_TYPE_NAMES.contains(resourceTypeName) && !setResource(view, isSrc, resId)) {
return resId;
}
}
return 0;
}
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 PictureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
if (attrs != null) {
final String namespace = "http://schemas.android.com/apk/res/android";
final String attribute = "src";
mResId = attrs.getAttributeResourceValue(namespace, attribute, 0);
}
}
public AddItemView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext=context;
Title=attrs.getAttributeValue(NAMESPACE,"aivtitle");
Desc=attrs.getAttributeValue(NAMESPACE, "aivdesc");
src=attrs.getAttributeResourceValue(NAMESPACE,"aivsrc",R.mipmap.icon_trangel);
initView();
}
private static StateListAnimator createStateListAnimatorFromXml(Context context,
XmlPullParser parser, AttributeSet attributeSet)
throws IOException, XmlPullParserException {
int type;
StateListAnimator stateListAnimator = new StateListAnimator();
while (true) {
type = parser.next();
switch (type) {
case XmlPullParser.END_DOCUMENT:
case XmlPullParser.END_TAG:
return stateListAnimator;
case XmlPullParser.START_TAG:
// parse item
Animator animator = null;
if ("item".equals(parser.getName())) {
int attributeCount = parser.getAttributeCount();
int[] states = new int[attributeCount];
int stateIndex = 0;
for (int i = 0; i < attributeCount; i++) {
int attrName = attributeSet.getAttributeNameResource(i);
if (attrName == R.attr.animation) {
final int animId = attributeSet.getAttributeResourceValue(i, 0);
animator = loadAnimator(context, animId);
} else {
states[stateIndex++] =
attributeSet.getAttributeBooleanValue(i, false) ?
attrName : -attrName;
}
}
if (animator == null) {
animator = createAnimatorFromXml(context.getResources(),
context.getTheme(), parser, 1f);
}
if (animator == null) {
throw new Resources.NotFoundException(
"animation state item must have a valid animation");
}
stateListAnimator
.addState(StateSet.trimStateSet(states, stateIndex), animator);
}
break;
}
}
}
@Override
protected void onInitAttributes(AttributeSet attrs) {
super.onInitAttributes(attrs);
if (isInEditMode()) {
// 为了在编译器中预览时不报空指针,在这里产生一个textView对象。实际中不会产生的。
textButton = new TextView(getContext());
}
String text = null;
/**
* 设置按钮上的文字内容
*/
int textResource = attrs.getAttributeResourceValue(ANDROIDXML,"text",-1);
if(textResource != -1){
text = getResources().getString(textResource);
}else{
//如果没有文字资源,也就是@String/xx,那么就设置文字
text = attrs.getAttributeValue(ANDROIDXML,"text");
}
/**
* 当文字不为空的时候,TextView设置文字,否则不设置文字
*/
if(text != null){
textButton.setText(text);
}
/**
* 设置textSize
*/
String textSize = attrs.getAttributeValue(ANDROIDXML,"textSize");
if (text != null && textSize != null) {
textSize = textSize.substring(0, textSize.length() - 2);//12sp->12
textButton.setTextSize(Float.parseFloat(textSize));
}
/**
* 设置textColor
*/
int textColor = attrs.getAttributeResourceValue(ANDROIDXML,"textColor",-1);
if(text != null && textColor != -1){
textButton.setTextColor(getResources().getColor(textColor));
}
else if(text != null ){
// 16进制的color
String color = attrs.getAttributeValue(ANDROIDXML,"textColor");
if(color != null && !isInEditMode()) {
textButton.setTextColor(Color.parseColor(color));
}else {
textButton.setTextColor(defaultTextColor);
}
}
textButton.setTypeface(null, Typeface.BOLD);
//textButton.setPadding(5, 5, 5, 5);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
params.setMargins(Utils.dpToPx(5, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(5, getResources()));
textButton.setLayoutParams(params);
addView(textButton);
}
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());
}
}
private void setBackground(AttributeSet attrs) {
final String xmlns = "http://schemas.android.com/apk/res/android";
xmlRes = attrs.getAttributeResourceValue(xmlns, "background", -1);
}