How to Use the custom attribute androidstyle In the android style File
A few days ago, I encountered a problem in the project: I wrote a custom control for the project, which will be reused in large quantities, so I am going to create a custom control in style. define a style in the xml file to reduce the compilation of repeated xml layout content, but there is a custom control attribute in it. The problem occurs here, although custom attributes can be used normally in layout xml, they cannot be defined in style. This control is used in large quantities. style is also used to reduce xml content writing, I can write the custom property content directly to the custom control, but considering the future variables of the project, I still want to write the custom property to the style, in this way, even if other styles are reused, I only need to write another style.
It is very easy to use custom attributes in layout.Xmlns: android = "http://schemas.android.com/apk/res/android"After adding the namespace of our custom control (chestnut: xmlns: test = "http://schemas.android.com/apk/res/packagename), then you can freely use custom properties in the custom control node.
When I was writing a style, I thought it was style at first. xml should also be used in this way, so the namespace of the custom control is added to the root node, and then the custom attributes are used in the style, as follows:
<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:test="http://schemas.android.com/res/com.zhufuing" > <style name="test" > <item name="test:name_text">hello,world!</item> </style></resources>
But this does not work. style. xml has an error, and the error message is as follows:
Error: Error: No resource found that matches the given name: attr 'test: name_text '.
After searching on the internet, I got the correct answer. In fact, we found the correct answer in style. if you use custom attributes in xml, you do not need to write the custom control namespace. You only need to replace the place where the naming control is used in the style with the package name of the custom control (note: is the package name, without the custom control name), as follows:
<resources xmlns:android="http://schemas.android.com/apk/res/android" > <style name="test" > <item name="com.zhufuing:name_text">hello,world!</item> </style></resources>
In this way, you can use custom attributes in the style file.
Reference link: http://www.eoeandroid.com/forum.php? Mod = viewthread & tid = 316876.