Custom view (custom attribute)

Source: Internet
Author: User

Custom view (custom attribute)
Jianghu langzhong makes a pure custom View (custom attribute) by the program designer. In the first two articles, we have introduced some methods to customize the View (inheriting the View) and custom views (composite controls), with custom controls, sometimes there is still a lack of flexible application, so some custom attributes need to be performed. To define the attributes, first define the required attributes. Create an xml file attrs In the res/values/directory. xml. This file defines all the attributes that need to be included. To illustrate this process, it defines an attr_title attribute. As shown below, attrs. xml 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <resources> 3 <declare-styleable name = "CombinView"> 4 <attr name = "attr_title" format = "string"> </attr> 5 </declare-styleable> 6 </resources> where: the values available for the format attribute include reference: refer to a resource ID color: color value boolean: boolean Value dimension: size value float: floating point value integer: integer value string: string fraction: Percentage enum: enumeration Value flag: bit or operation above can be used separately or in combination, such as reference | color, separated by "|" in the middle, attribute application attributes are defined, the following describes how to use it. Copy the Code 1 <RelativeLayout xmlns: android = "http: // Schemas.android.com/apk/res/android "2 xmlns: tools =" http://schemas.android.com/tools "3 xmlns: cview =" http://schemas.android.com/apk/res/com.example.testcview "4 android: layout_width =" match_parent "5 android: layout_height =" match_parent "6 tools: context = "$ {packageName }. $ {activityClass} "7 8> 9 10 <com. example. testcview. combinView11 android: id = "@ + id/combinView1" 12 android: layout_width = "wrap_conten T "13 android: layout_height =" wrap_content "14 android: layout_alignParentRight =" true "15 android: layout_alignParentTop =" true "16 cview: attr_title = "Custom question Header" 17> 18 </com. example. testcview. combinView> 19 20 </RelativeLayout> copy the Code which has two points to note, 1> xmlns: cview = "http://schemas.android.com/apk/res/com.example.testcview", need to declare your own namespace, the previous part is done 2> Set Custom Attributes on the custom control. For cview: attr_title = "Custom question Header", add your own naming control, in this way, the custom property is applied to the custom control. The Java code obtains and sets the property content. In the custom control constructor, obtain the set value and set it to the corresponding position, copy code 1 public class CombinView extends RelativeLayout {2 3 public CombinView (Context context, AttributeSet attrs) {4 super (context, attrs); 5 LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); 6 RelativeLayout layout = (RelativeLayout) inflater. inflate (R. layout. custom_view, this); 7 8 // get custom attributes 9 TypedArray tArray = context. obtainStyledAttributes (attrs, R. styleable. combinView); 10 String title = tArray. getString (R. styleable. combinView_attr_title); 11 12 TextView tvTitle = (TextView) layout. findViewById (R. id. tvTitle); 13 System. out. println ("title =" + title); 14 tvTitle. setText (title); 15 16 tArray. recycle (); 17} 18 19} copy the code, and use TypedArray tArray = context. obtainStyledAttributes (attrs, R. styleable. combinView); to get the custom attribute content, and then use String title = tArray. getString (R. styleable. combinView_attr_title); obtain the value of the specified attribute so that we can remove the value of the custom attribute. The specific usage depends on the specific requirements.

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.