Android custom View and layout attributes

Source: Internet
Author: User
Home Page> Android development> full introduction to custom View and layout attributes of Android
  • Release date: 2010-08-10
  • Author:Original Android Development Network

You may be familiar with the custom View of the Android system. Many netizens are not very familiar with the attribute addition of the custom View and the namespace of the Android Layout, today, Android123 will take you back to study.

CwjView myView = new CwjView (context );

If the interface is used for a game or the entire form, we may directly setContentView (myView) in onCreate; of course, if it is a control, we may need to declare it from the xml of Layout, such

<Cn.com. android123.CwjView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>

Of course, we can also directly declare

<View class = "cn.com. android123.CwjView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>

We only use the two attributes of the parent class View, all from the android namespace, And the name is layout_width or layout_height. Our custom control may have more functions, such

<Cn.com. android123.CwjView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
 Cwj: age = "22"
Cwj: university = "sjtu"
Cwj: city = "shanghai"
/>

We can see that the preceding three attributes are customized. As a standard xml specification, it may also include xmlns:Android= "Http://schemas.android.com/apk/res/Android"For a fully defined View, the namespace is cwj, which can be written as xmlns:Cwj= Http://schemas.android.com/apk/res/Cn.com. android123.cwjViewOr xmlns:Cwj= Http://schemas.android.com/apk/res/AndroidYes.

How do we define the definition of the cwj namespace and the three attributes of age, university, and city? In the res/values directory of the project, we create a new cwj_attr.xml file. It is a good habit to encode UTF-8 as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Declare-styleable name = "CwjView">
<Attr name = "age" format = "integer"/>
<Attr name = "city" format = "string"/>
<Attr name = "university" format = "string"/>
</Declare-styleable>
</Resources>

 
Here we may not be very familiar with the format. Currently, the built-in format types of Android include integer, for example, progress value of ProgressBar, float
The value of RatingBar may be 3.5 stars. boolean, such as whether ToggleButton is checked, string, such as TextView's text attribute
However, in addition to our common basic types, there are also special Android attributes, such as color, which can be recognized as # FF0000.
The dimension type of dimension, such as the unit of length of 23dip, 15px, and 18sp, and a special reference, which is generally used to reference @ + id/cwj
@ Drawable/xxx.

When will reference be used? Let's take defining a color as an example,

<Attr name = "red" format = "color | reference"/> here we use the logical OR operator. The red color is defined and can be referenced.

Of course, for our custom class, we need to use a method named obtainStyledAttributes to get our definition. You can use

Public CwjView (Context context, AttributeSet attrs ){
Super (context, attrs );
TypedArray a = context. obtainStyledAttributes (attrs,
R. styleable. cwj_attr );
MAge = a. getInteger (R. styleable. CwjView_age, 22 );
MCity = a. getString (R. styleable. CwjView_city, "shanghai ");
MUniversity = a. getString (R. styleable. CwjView_university, "sjtu ");

A. recycle (); // Android123 reminds you not to forget to recycle resources.

}

In this way, the global member variables mAge and mCity of the class obtain the content we need. Of course, according to the value in layout, the custom CwjView needs to dynamically process some data, you can use the getAttributeResourceValue method of the AttributeSet class to obtain the value.

Public CwjView (Context context, AttributeSet attrs)
{
Super (context, attrs );
ResId = attrs. getAttributeResourceValue ("cn.com. android123.CwjView", "age", 100 );
ResId = attrs. getAttributeResourceValue ("cn.com. android123.CwjView", "city", "shanghai ");
// ResID can be used at will
}

In the above two methods, the last value of the parameter is the default, if you do not understand the place can be written to the android123@163.com we will reply in the first time.

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.