How to use custom properties in an Android style file

Source: Internet
Author: User

The other day I ran into a problem with the project: I wrote a custom control for my project, and the control was heavily reused, So I'm going to define a style in the Style.xml file to reduce the duplication of the XML layout content, but there's a custom control property, and the problem is here, although the custom property can be used in layout XML, but it can't be defined in the style, which is a lot of taking. , style is also written to reuse the reduced XML content, I can write the custom attribute content directly to die in the custom control, but considering the possible future variables of the project, I am prepared to write this custom attribute to the style, so that even if there are other different styles of reuse, I just have to write a style.

The use of custom properties in layout layouts is very simple and we only need to xmlns:android= "Http://schemas.android.com/apk/res/android" on the XML root node followed by our custom control's namespace (chestnut: xmlns:test= "http://schemas.android.com/apk/res/packagename), then you can freely use custom properties in the custom control node.

When I wrote the style, I thought that style.xml should be the way to use it at first, so I added the namespace of the custom control to the root node and started using the custom property 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>

However, this does not work, style.xml on the contrary, error prompts as follows:

Error:Error:No resource found that matches the given name:attr ' Test:name_text '.

I got the right answer after I searched the internet, and instead of writing a custom control's namespace, we didn't need to write the name space for the Style.xml, we just need to replace the name of the custom control with the one in the style (note: The package name, without the name of the custom control). 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>

This allows you to use a custom attribute in the style file.


Reference Link: http://www.eoeandroid.com/forum.php?mod=viewthread&tid=316876

How to use custom properties in an Android style file

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.