Declare-styleable Property Reuse Scheme for custom controls in Android _android

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.