Custom Layout Resources

You can define custom elements to use in layout resources. These custom elements can then be used the same as any Android layout elements: that is, you can use them and specify their attributes in other resources. The ApiDemos sample application has an example of creating a custom layout XML tag, LabelView. To create a custom element, you will need the following files:

• Java implementation file - The implementation file. The class must extend View or a subclass. See in ApiDemos.

• res/values/attrs.xml - Defines the XML element, and the attributes that it supports, for clients to use to instantiate your object in their layout XML file. Define your element in a <declare-styleable riame=your_java_class_name>. See res/values/attrs.xml in ApiDemos.

• res/layout/your_c/ass.xml [optional] - An optional XML file to describe the layout of your object. This could also be done in Java. See custom_view_1.xml in ApiDemos.

Source file format: XML file without an <?xml> declaration, and a <resources> root element containing one or more custom element tags. Resource file location: res/values/attrs.xml (File name is arbitrary.) Compiled resource datatype: Resource pointer to a View (or subclass) resource. Resource reference name: R.styleable.some_file (Java).

0 0

Post a comment