Getting Style Attributes Dynamically
Solution 1:
There isn't really anything special about the styleable array. All obtainStyledAttributes()
does is allow you to obtain a batch of attributes in one call, instead of having to perform a separate call for each attribute, which is slower. So the styleable array is just an array of resource identifiers for attributes. There are special requirements on how it is structured -- the resource identifiers need to be in sorted order, as this is part of the optimization to quickly retrieving them. So if you want to build your own array, you need to make sure that the identifiers you put there are appropriately sorted. If you don't know the identifier numbers, you can use the code you showed to get the value for each one... though note that doing all of that setup each time you call obtainStyledAttributes()
probably more than removes the performance gain you would get from the function.
Solution 2:
You could try
TypedValueoutValue=newTypedValue();
context.getTheme().resolveAttribute( R.attr.someAttribute, outValue, true );
return outValue.resourceId;
Post a Comment for "Getting Style Attributes Dynamically"