Custom properties for Android Custom Controls

Source: Internet
Author: User
Tags xml attribute custom name

The original attributes of Android may not meet our current needs. After all, some people may come up with something that android will not do. Today, I will briefly write down how to define custom properties for custom controls. It seems a bit dizzy to see this description. In turn, the control and attributes are both custom. Ha ~

The above is the running interface, which has two custom buttons, mainly used for distinguishing.

Firstres/values/Create an attrs. xml file in the directory and use the Custom Attributes

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="MyCustomWidget">        <attr name="myText" format="string"/>        <attr name="isEnable" format="boolean"/>    </declare-styleable></resources>

The name value of the tag declare-styleable is that you can use this value to obtain relevant attributes, which indicates that these attributes are assigned to custom controls.

The following code reads the set value from the XML Attribute in the constructor of mymwmwidget. java. You can use the obtained value to do your own work. The Code is as follows:

Package NBE. sense7.vinci. custom. xmlattrs; import android. content. context; import android. content. res. typedarray; import android. util. attributeset; import android. widget. button; public class customwedgit extends button {public customwedgit (context, attributeset attrs) {super (context, attrs); // todo auto-generated constructor stub typedarray A = context. obtainstyledattributes (attrs, R. styleable. mycustomwidget); Final int indexcount =. getindexcount (); For (INT I = 0; I <indexcount; ++ I) {int ATTR =. getindex (I); Switch (ATTR) {case R. styleable. mycustomwidget_mytext: // obtain the mytext attribute value string mytext =. getstring (ATTR); // after obtaining the value, you can use it as you want. Here I will display the customwedgit text for the button setting button. this. settext (mytext); break; case R. styleable. mycustomwidget_isenable: Boolean isenable =. getboolean (ATTR, false); // use the Boolean attribute value to do the break you want to do here;}. recycle ();}}

Finally, add the custom attributes to layout. Main. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Vinci = "http://schemas.android.com/apk/res/nbe.sense7.vinci.custom.xmlattrs" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: orientation = "vertical"> <NBE. sense7.vinci. custom. xmlattrs. customwedgit Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "the value set by the text attribute of the button"/> <NBE. sense7.vinci. custom. xmlattrs. customwedgit Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Vinci: mytext = "button custom mytext attribute setting value"/> </linearlayout>

The most important thing is that you should not forget to add a dark line above. xmlns: you can add a custom name to the end. My name is vinici. The value in the quotation marks should be xmlns: vinci = "http://schemas.android.com/apk/res/your package name ".

OK ~ Hand-written !!

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.