Android's styling feature is meant for just one style to be applied to a view. This works just fine in most cases. However, there are times when properties from multiple styles have to be applied to a view. This makes sense only when the desired properties isn't provided in these styles. Ideally, application developers can just have one style extend from another style and get done. However, this is a challenge when one of these styles is a non-public platform style. Forking the platform style into application project is limited and doesn't necessarily work in platforms having different implementations. Besides, its not future proof as the stock AOSP version from Google might change the style implementation in a future release.
In case of non-public platform style, android usually exposes a style attribute to customize the look and feel of it. Applications are supposed to provide a custom style implementation for this style attribute and that is how the platform style is ignored. Application developers would like to extend the platform style but can't as its not public. So is there any way to achieve this?
Unfortunately, it can't be done just through the XML. It needs a combination of both XML and source code. Developers can apply the first style ( usually defined in the application ) in XML and can extract the property values for the attributes controlled by the platform style in code using
obtainStyledAttributes (int resid, int[] attrs)
attrs is an integer array representing the set of attributes that we would like to resolve from the style. resId needs to be the actual style resource id and not a style attribute id. As the platform style isn't public, we need the following logic to find the id of the actual style.
TypedValue resolvedValue = new TypedValue();
context.getTheme().resolveAttributes( R.attr.referenceAttrName, resolvedValue, true );
int actualResourceId = resolvedValue.resourceId;
Now, actualResourceId is an integer representing the non-public platform style. We can basically extract the desired properties from this resource using obtainStyledAttributes. And once we have the values, its just a question of finding the appropriate view APIs and calling them on the desired view.
In case of non-public platform style, android usually exposes a style attribute to customize the look and feel of it. Applications are supposed to provide a custom style implementation for this style attribute and that is how the platform style is ignored. Application developers would like to extend the platform style but can't as its not public. So is there any way to achieve this?
Unfortunately, it can't be done just through the XML. It needs a combination of both XML and source code. Developers can apply the first style ( usually defined in the application ) in XML and can extract the property values for the attributes controlled by the platform style in code using
obtainStyledAttributes (int resid, int[] attrs)
attrs is an integer array representing the set of attributes that we would like to resolve from the style. resId needs to be the actual style resource id and not a style attribute id. As the platform style isn't public, we need the following logic to find the id of the actual style.
TypedValue resolvedValue = new TypedValue();
context.getTheme().resolveAttributes( R.attr.referenceAttrName, resolvedValue, true );
int actualResourceId = resolvedValue.resourceId;
Now, actualResourceId is an integer representing the non-public platform style. We can basically extract the desired properties from this resource using obtainStyledAttributes. And once we have the values, its just a question of finding the appropriate view APIs and calling them on the desired view.
No comments:
Post a Comment