The recent exposure to the Android custom control, which involves customizing attributes in XML, is also very simple, but it is written to show that the code is not perfect, and that the attribute redundancy is found in the Attrs.xml file, So you want to have a similar attribute inheritance or include such methods. This article will record the property reuse in Declare-stylable.
Not perfect code.
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= "Extextview" >
<attr name= "Enableonpad" format= "boolean"/>
<attr name= "Supportdevicetype" format= "Reference"/>
</declare-styleable>
<declare-styleable name= "Exedittext" >
<attr name= "Enableonpad" format= "boolean"/>
<attr name= "Supportdevicetype" format= "Reference"/>
</declare-styleable>
</resources>
As in the above code, there are duplicate attribute declarations in the Extextview and Exedittext stylable. Although it can work, but always feel that writing is not professional, so look for optimization methods.
Is that OK?
Try to specify a parent for declare-stylable, the following code
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= "Extextview" >
<attr name= "Enableonpad" format= "boolean"/>
<attr name= "Supportdevicetype" format= "Reference"/>
</declare-styleable>
<declare-styleable name= "Exedittext" parent= "Extextview" >
</declare-styleable>
</resources>
Attrs.xml did not report a syntax error. But when I use R.styleable.exedittext_supportdevicetype, R file is not generated, clean the project or not effective, I don't know if it's a problem with the ADT plugin. Others have encountered such problems. This method is not available at present.
The ultimate Answer
In fact, we can declare the attributes to be used more than once before declare-stylable, within the declare-stylable node, just call. The specific code is as follows.
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<attr name= "Enableonpad" format= "boolean"/>
<attr name= "Supportdevicetype" format= "Reference"/>
<declare-styleable name= "Extextview" >
<attr name= "Enableonpad"/>
<attr name= "Supportdevicetype"/>
</declare-styleable>
<declare-styleable name= "Exedittext" >
<attr name= "Enableonpad"/>
<attr name= "Supportdevicetype"/>
</declare-styleable>
</resources>
After each reference to the attr, it is recommended that you clean up the project and make sure the R file is regenerated.
Extended Reading
Http://stackoverflow.com/questions/18827875/ How-to-declare-several-stylable-attributes-with-the-same-name-for-different-tags