下面列出了android.util.AttributeSet#getAttributeName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public TextViewPreference(Context context, AttributeSet attrs)
{
super(context, attrs);
for(int i = 0; i < attrs.getAttributeCount(); i++) {
String attr = attrs.getAttributeName(i);
String val = attrs.getAttributeValue(i);
if("title".equals(attr)) {
this.title = context.getString(Integer.parseInt(val.substring(1)));
}
if("summary".equals(attr)) {
this.description = context.getString(Integer.parseInt(val.substring(1)));
}
if("clickable".equals(attr) && "false".equals(val)) {
this.setEnabled(false);
}
}
}
/**
* 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);
}
public static List<Attr> getNightModelAttr(Object[] args, Resources resources) {
List<Attr> nightModelAttrs = new ArrayList<>();
if (args != null && args.length > 0) {
for (Object obj: args) {
if (obj instanceof AttributeSet) {
AttributeSet attrs = (AttributeSet) obj;
for (int i = 0; i < attrs.getAttributeCount(); i++)
{
String attrName = attrs.getAttributeName(i);
String attrValue = attrs.getAttributeValue(i);
AttrType attrType = getSupportAttrType(attrName);
if (attrType == null) continue;
if (attrValue.startsWith("@")) {
String resourceName = attrType.getResourceName(attrValue, resources);
Attr attr = new Attr(resourceName, attrType);
nightModelAttrs.add(attr);
}
}
}
}
}
return nightModelAttrs;
}
/**
* 检查控件的属性值是否有对主题属性的引用,如果有则需要换肤
*
* @param view
* @param attrs
* @return
*/
public boolean addElement(View view, AttributeSet attrs) {
if (view == null) {
return false;
}
SkinElement skinElement = new SkinElement(view);
int count = attrs.getAttributeCount();
for (int i = 0; i < count; i++) {
String name = attrs.getAttributeName(i);
String value = attrs.getAttributeValue(i);
if (!TextUtils.isEmpty(value) && value.startsWith("?")) {
try {
String temp = value.substring(1);
int attrId = Integer.parseInt(temp);
skinElement.changeAttrs.put(name, attrId);
} catch (Exception e) {
}
}
}
if (skinElement.changeAttrs.size() == 0) {
return false;
}
skinElements.add(skinElement);
return true;
}
public PreviewEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
summaryTemplate = super.getSummary();
for (int i = 0; i < attrs.getAttributeCount(); i++) {
String attr = attrs.getAttributeName(i);
String val = attrs.getAttributeValue(i);
if (attr.equalsIgnoreCase("previewTemplate")) {
previewTemplate = val;
}
}
}
public PreviewListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
summaryTemplate = super.getSummary();
for (int i = 0; i < attrs.getAttributeCount(); i++) {
String attr = attrs.getAttributeName(i);
String val = attrs.getAttributeValue(i);
if (attr.equalsIgnoreCase("previewTemplate")) {
previewTemplate = val;
}
}
}
public NS_MOBILE_EXTRA(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.preference_seekbar);
for (int i = 0; i < attrs.getAttributeCount(); i++) {
String attr = attrs.getAttributeName(i);
if (attr.equalsIgnoreCase("pref_kind")) {
prefKind = attrs.getAttributeValue(i);
break;
}
}
}
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
if (!WidgetView.class.getName().equals(name)) {
try {
return createView(name, context, attrs);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
AppCompatActivity activity = ContextUtils.getAppCompatActivity(context);
if (null == activity) {
throw new IllegalStateException("please use AppCompatActivity context to inflate");
}
// create widget
String widgetName = null;
String params = null;
int id = 0;
int count = attrs.getAttributeCount();
for (int i = 0; i < count; i++) {
String attrName = attrs.getAttributeName(i);
String attrValue = attrs.getAttributeValue(i);
if ("widget".equals(attrName)) {
widgetName = attrValue;
} else if ("params".equals(attrName)) {
params = attrValue;
} else if ("id".equals(attrName)) {
id = Integer.parseInt(attrValue.substring(1));
}
}
Widget widget = WidgetInflateUtils.createWidget(widgetName, activity, activity.getLifecycle());
WidgetInflateUtils.initWidgetWithParams(context, widget, params);
View view = widget.render();
view.setTag(widget);
view.setId(id);
return view;
}
public RangePreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.preference_range);
for (int i = 0; i < attrs.getAttributeCount(); i++) {
String attr = attrs.getAttributeName(i);
if (attr.equalsIgnoreCase("start")) {
startKey = attrs.getAttributeValue(i);
} else if (attr.equalsIgnoreCase("end")) {
endKey = attrs.getAttributeValue(i);
}
}
}
/**
* 把这个View的属性中需要的(SkinType, entryName)对取出来(作为SkinAttr类)
* @param context
* @param attrs
* @return
*/
public static List<SkinAttr> getAttrs(Context context, AttributeSet attrs) {
//把background、src、textColor解析出来
List<SkinAttr> result = new ArrayList<>();
//开始解析所有属性
int length = attrs.getAttributeCount();
for (int i=0; i<length; i++){
//获取属性名称
String name = attrs.getAttributeName(i);
//获取属性值
String value = attrs.getAttributeValue(i);
//Log.e(TAG, name + " "+ value);
//检查是否存在所需属性名称
SkinType skinType = getTargetSkinType(name);
//如果是所需要的属性名
if(skinType != null){
//获取属性值名称
String resName = getResName(context, value);
if(TextUtils.isEmpty(resName)){
continue;
}
SkinAttr attr = new SkinAttr(resName, skinType);
result.add(attr);
}
}
return result;
}
public NS_MOBILE_EXTRA(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.preference_seekbar);
for (int i = 0; i < attrs.getAttributeCount(); i++) {
String attr = attrs.getAttributeName(i);
if (attr.equalsIgnoreCase("pref_kind")) {
prefKind = attrs.getAttributeValue(i);
break;
}
}
}
public RangePreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.preference_range);
for (int i = 0; i < attrs.getAttributeCount(); i++) {
String attr = attrs.getAttributeName(i);
if (attr.equalsIgnoreCase("start")) {
startKey = attrs.getAttributeValue(i);
} else if (attr.equalsIgnoreCase("end")) {
endKey = attrs.getAttributeValue(i);
}
}
}
public static List<SkinAttr> getSkinAttrs(AttributeSet attrs, Context context)
{
List<SkinAttr> skinAttrs = new ArrayList<SkinAttr>();
SkinAttr skinAttr = null;
for (int i = 0; i < attrs.getAttributeCount(); i++)
{
String attrName = attrs.getAttributeName(i);
String attrValue = attrs.getAttributeValue(i);
SkinAttrType attrType = getSupprotAttrType(attrName);
if (attrType == null) continue;
if (attrValue.startsWith("@"))
{
int id = Integer.parseInt(attrValue.substring(1));
String entryName = context.getResources().getResourceEntryName(id);
L.e("entryName = " + entryName);
if (entryName.startsWith(SkinConfig.ATTR_PREFIX))
{
skinAttr = new SkinAttr(attrType, entryName);
skinAttrs.add(skinAttr);
}
}
}
return skinAttrs;
}
/**
* 把这个View的属性中需要的(SkinType, entryName)对取出来(作为SkinAttr类)
* @param context
* @param attrs
* @return
*/
public static List<SkinAttr> getAttrs(Context context, AttributeSet attrs) {
//把background、src、textColor解析出来
List<SkinAttr> result = new ArrayList<>();
//开始解析所有属性
int length = attrs.getAttributeCount();
for (int i=0; i<length; i++){
//获取属性名称
String name = attrs.getAttributeName(i);
//获取属性值
String value = attrs.getAttributeValue(i);
//Log.e(TAG, name + " "+ value);
//检查是否存在所需属性名称
SkinType skinType = getTargetSkinType(name);
//如果是所需要的属性名
if(skinType != null){
//获取属性值名称
String resName = getResName(context, value);
if(TextUtils.isEmpty(resName)){
continue;
}
SkinAttr attr = new SkinAttr(resName, skinType);
result.add(attr);
}
}
return result;
}
private void loadFonts(Context context, @Nullable AttributeSet attrs) {
_defaultValue = _fontValues[0];
if (attrs != null) {
for (int i = 0; i < attrs.getAttributeCount(); i++) {
String attrName = attrs.getAttributeName(i);
String attrValue = attrs.getAttributeValue(i);
if (attrName.equalsIgnoreCase("defaultValue")) {
if (attrValue.startsWith("@")) {
int resId = Integer.valueOf(attrValue.substring(1));
attrValue = getContext().getString(resId);
}
_defaultValue = attrValue;
break;
}
}
}
Spannable[] fontText = new Spannable[_fontNames.length];
for (int i = 0; i < _fontNames.length; i++) {
fontText[i] = new SpannableString(_fontNames[i] + "\n" + _fontValues[i]);
fontText[i].setSpan(new TypefaceSpan(_fontValues[i]), 0, _fontNames[i].length(), 0);
fontText[i].setSpan(new RelativeSizeSpan(0.7f), _fontNames[i].length() + 1, fontText[i].length(), 0);
}
setDefaultValue(_defaultValue);
setEntries(fontText);
setEntryValues(_fontValues);
}
private static PackageLite parsePackageLite(Resources res, XmlPullParser parser,
AttributeSet attrs, int flags, String[] outError) throws IOException,
XmlPullParserException {
int type;
while ((type = parser.next()) != XmlPullParser.START_TAG
&& type != XmlPullParser.END_DOCUMENT) {
;
}
if (type != XmlPullParser.START_TAG) {
outError[0] = "No start tag found";
return null;
}
if (DEBUG_PARSER)
Slog.v(TAG, "Root element name: '" + parser.getName() + "'");
if (!parser.getName().equals("manifest")) {
outError[0] = "No <manifest> tag";
return null;
}
String pkgName = attrs.getAttributeValue(null, "package");
if (pkgName == null || pkgName.length() == 0) {
outError[0] = "<manifest> does not specify package";
return null;
}
String nameError = validateName(pkgName, true);
if (nameError != null && !"android".equals(pkgName)) {
outError[0] = "<manifest> specifies bad package name \""
+ pkgName + "\": " + nameError;
return null;
}
int installLocation = PARSE_DEFAULT_INSTALL_LOCATION;
for (int i = 0; i < attrs.getAttributeCount(); i++) {
String attr = attrs.getAttributeName(i);
if (attr.equals("installLocation")) {
installLocation = attrs.getAttributeIntValue(i,
PARSE_DEFAULT_INSTALL_LOCATION);
break;
}
}
// Only search the tree when the tag is directly below <manifest>
final int searchDepth = parser.getDepth() + 1;
final List<VerifierInfo> verifiers = new ArrayList<VerifierInfo>();
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
&& (type != XmlPullParser.END_TAG || parser.getDepth() >= searchDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
if (parser.getDepth() == searchDepth && "package-verifier".equals(parser.getName())) {
final VerifierInfo verifier = parseVerifier(res, parser, attrs, flags, outError);
if (verifier != null) {
verifiers.add(verifier);
}
}
}
return new PackageLite(pkgName.intern(), installLocation, verifiers);
}
private static PackageLite parsePackageLite(Resources res, XmlPullParser parser,
AttributeSet attrs, int flags, String[] outError) throws IOException,
XmlPullParserException {
int type;
while ((type = parser.next()) != XmlPullParser.START_TAG
&& type != XmlPullParser.END_DOCUMENT) {
;
}
if (type != XmlPullParser.START_TAG) {
outError[0] = "No start tag found";
return null;
}
if (DEBUG_PARSER)
Slog.v(TAG, "Root element name: '" + parser.getName() + "'");
if (!parser.getName().equals("manifest")) {
outError[0] = "No <manifest> tag";
return null;
}
String pkgName = attrs.getAttributeValue(null, "package");
if (pkgName == null || pkgName.length() == 0) {
outError[0] = "<manifest> does not specify package";
return null;
}
String nameError = validateName(pkgName, true);
if (nameError != null && !"android".equals(pkgName)) {
outError[0] = "<manifest> specifies bad package name \""
+ pkgName + "\": " + nameError;
return null;
}
int installLocation = PARSE_DEFAULT_INSTALL_LOCATION;
for (int i = 0; i < attrs.getAttributeCount(); i++) {
String attr = attrs.getAttributeName(i);
if (attr.equals("installLocation")) {
installLocation = attrs.getAttributeIntValue(i,
PARSE_DEFAULT_INSTALL_LOCATION);
break;
}
}
// Only search the tree when the tag is directly below <manifest>
final int searchDepth = parser.getDepth() + 1;
final List<VerifierInfo> verifiers = new ArrayList<VerifierInfo>();
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
&& (type != XmlPullParser.END_TAG || parser.getDepth() >= searchDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
if (parser.getDepth() == searchDepth && "package-verifier".equals(parser.getName())) {
final VerifierInfo verifier = parseVerifier(res, parser, attrs, flags, outError);
if (verifier != null) {
verifiers.add(verifier);
}
}
}
return new PackageLite(pkgName.intern(), installLocation, verifiers);
}
private static PackageLite parsePackageLite(Resources res, XmlPullParser parser,
AttributeSet attrs, int flags, String[] outError) throws IOException,
XmlPullParserException {
int type;
while ((type = parser.next()) != XmlPullParser.START_TAG
&& type != XmlPullParser.END_DOCUMENT) {
;
}
if (type != XmlPullParser.START_TAG) {
outError[0] = "No start tag found";
return null;
}
if (DEBUG_PARSER)
Slog.v(TAG, "Root element name: '" + parser.getName() + "'");
if (!parser.getName().equals("manifest")) {
outError[0] = "No <manifest> tag";
return null;
}
String pkgName = attrs.getAttributeValue(null, "package");
if (pkgName == null || pkgName.length() == 0) {
outError[0] = "<manifest> does not specify package";
return null;
}
String nameError = validateName(pkgName, true);
if (nameError != null && !"android".equals(pkgName)) {
outError[0] = "<manifest> specifies bad package name \""
+ pkgName + "\": " + nameError;
return null;
}
int installLocation = PARSE_DEFAULT_INSTALL_LOCATION;
int versionCode = 0;
int numFound = 0;
for (int i = 0; i < attrs.getAttributeCount(); i++) {
String attr = attrs.getAttributeName(i);
if (attr.equals("installLocation")) {
installLocation = attrs.getAttributeIntValue(i,
PARSE_DEFAULT_INSTALL_LOCATION);
numFound++;
} else if (attr.equals("versionCode")) {
versionCode = attrs.getAttributeIntValue(i, 0);
numFound++;
}
if (numFound >= 2) {
break;
}
}
// Only search the tree when the tag is directly below <manifest>
final int searchDepth = parser.getDepth() + 1;
final List<VerifierInfo> verifiers = new ArrayList<VerifierInfo>();
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
&& (type != XmlPullParser.END_TAG || parser.getDepth() >= searchDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
if (parser.getDepth() == searchDepth && "package-verifier".equals(parser.getName())) {
final VerifierInfo verifier = parseVerifier(res, parser, attrs, flags, outError);
if (verifier != null) {
verifiers.add(verifier);
}
}
}
return new PackageLite(pkgName.intern(), versionCode, installLocation, verifiers);
}
private static PackageLite parsePackageLite(Resources res, XmlPullParser parser,
AttributeSet attrs, int flags, String[] outError) throws IOException,
XmlPullParserException {
int type;
while ((type = parser.next()) != XmlPullParser.START_TAG
&& type != XmlPullParser.END_DOCUMENT) {
;
}
if (type != XmlPullParser.START_TAG) {
outError[0] = "No start tag found";
return null;
}
if (DEBUG_PARSER)
Slog.v(TAG, "Root element name: '" + parser.getName() + "'");
if (!parser.getName().equals("manifest")) {
outError[0] = "No <manifest> tag";
return null;
}
String pkgName = attrs.getAttributeValue(null, "package");
if (pkgName == null || pkgName.length() == 0) {
outError[0] = "<manifest> does not specify package";
return null;
}
String nameError = validateName(pkgName, true);
if (nameError != null && !"android".equals(pkgName)) {
outError[0] = "<manifest> specifies bad package name \""
+ pkgName + "\": " + nameError;
return null;
}
int installLocation = PARSE_DEFAULT_INSTALL_LOCATION;
int versionCode = 0;
int numFound = 0;
for (int i = 0; i < attrs.getAttributeCount(); i++) {
String attr = attrs.getAttributeName(i);
if (attr.equals("installLocation")) {
installLocation = attrs.getAttributeIntValue(i,
PARSE_DEFAULT_INSTALL_LOCATION);
numFound++;
} else if (attr.equals("versionCode")) {
versionCode = attrs.getAttributeIntValue(i, 0);
numFound++;
}
if (numFound >= 2) {
break;
}
}
// Only search the tree when the tag is directly below <manifest>
final int searchDepth = parser.getDepth() + 1;
final List<VerifierInfo> verifiers = new ArrayList<VerifierInfo>();
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
&& (type != XmlPullParser.END_TAG || parser.getDepth() >= searchDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
if (parser.getDepth() == searchDepth && "package-verifier".equals(parser.getName())) {
final VerifierInfo verifier = parseVerifier(res, parser, attrs, flags, outError);
if (verifier != null) {
verifiers.add(verifier);
}
}
}
return new PackageLite(pkgName.intern(), versionCode, installLocation, verifiers);
}
private static boolean parseApplication(PackageLite packageLite,
XmlPullParser xmlPullParser, AttributeSet attributeSet)
throws Exception {
int i;
String str = packageLite.packageName;
for (i = 0; i < attributeSet.getAttributeCount(); i++) {
String attributeName = attributeSet.getAttributeName(i);
if (attributeName.equals("name")) {
packageLite.applicationClassName = buildClassName(str,
attributeSet.getAttributeValue(i));
} else if (attributeName.equals("icon")) {
packageLite.applicationIcon = attributeSet
.getAttributeResourceValue(i, 0);
} else if (attributeName.equals("label")) {
packageLite.applicationLabel = attributeSet
.getAttributeResourceValue(i, 0);
} else if (attributeName.equals("description")) {
packageLite.applicationDescription = attributeSet
.getAttributeResourceValue(i, 0);
}
}
final int innerDepth = xmlPullParser.getDepth();
int type;
while ((type = xmlPullParser.next()) != XmlPullParser.END_DOCUMENT
&& (type != XmlPullParser.END_TAG || xmlPullParser.getDepth() > innerDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
String tagName = xmlPullParser.getName();
if (tagName.equals("activity")) {
parseComponentData(packageLite, xmlPullParser,
attributeSet, false);
} else if (tagName.equals("receiver")) {
parseComponentData(packageLite, xmlPullParser,
attributeSet, true);
} else if (tagName.equals("service")) {
parseComponentData(packageLite, xmlPullParser,
attributeSet, true);
} else if (tagName.equals("provider")) {
parseComponentData(packageLite, xmlPullParser,
attributeSet, false);
} else if (tagName.equals("activity-alias")) {
} else if (xmlPullParser.getName().equals("meta-data")) {
packageLite.metaData = parseMetaData(xmlPullParser,
attributeSet, packageLite.metaData);
} else if (tagName.equals("uses-library")) {
} else if (tagName.equals("uses-package")) {
} else {
}
}
return true;
}