Android Custom ViewGroup and custom properties

Source: Internet
Author: User

One, custom ViewGroup:

1. Choice of construction method:

Get some values (some properties or custom properties) that you need to use

Public Custonviewgroup (Context context) {This (context, null);}

Public Custonviewgroup (context context, AttributeSet Attrs) {This (context, attrs, 0);}

Attrs declared in the layout file, the above two construction methods cannot have custom properties

Public Custonviewgroup (context context, AttributeSet attrs, int defstyleattr)

This construction method is called when there are custom attributes


2. onmeasure:

Calculates the width and height of the sub-view and sets its own width and height


3. OnLayout:

Determine the location of the child view's layout


4. ontouchevent:

Listen for touch events, such as finger press or release, and decide whether or not to overload the method as needed


Second, custom attributes:
1, create Attr.xml file:650) this.width=650; "Src=" http://s1.51cto.com/wyfs02/M02/7A/4C/ Wkiom1anfbrsladvaaancu2zovs723.png "title=" qq picture 20160126144328.png "alt=" Wkiom1anfbrsladvaaancu2zovs723.png "/>

Define the relevant properties in the file, for example:

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>

<attr name= "rightpadding" format= "Dimension" ></attr>

<declare-styleable name= "Slidingmenu" >
<attr name= "Rightpadding" ></attr>
</declare-styleable>

</resources>


Where attr name is a custom property name, declare-styleable name is a custom viewgroup name


2. Use in Layout files:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7A/4D/wKioL1anGxnyMv5jAABuuJD5Y9s312.png "title=" QQ picture 20160126150535.png "alt=" Wkiol1angxnymv5jaabuujd5y9s312.png "/>

Copy the first part of the Xmlns:android "http://schemas.android.com/apk", the latter part with "/res/Package name" or "/res-auto".


3. Get the value of the custom attribute in the construction method of 3 parameters:

Get the properties we define
TypedArray a = Context.gettheme (). Obtainstyledattributes (Attrs, R.styleable.slidingmenu, defstyleattr,0);
int n = a.getindexcount ();
for (int i = 0; i < n; i++) {
int attr = A.getindex (i);
Switch (attr) {
Case r.styleable.slidingmenu_rightpadding:

mmenurightpadding = A.getdimensionpixeloffset (attr,

(int) Typedvalue.applydimension (Typedvalue.complex_unit_dip, Context.getresources (), Getdisplaymetrics ());

Break
}
}
A.recycle ();


This article is from "Snail Journey" blog, please be sure to keep this source http://8432499.blog.51cto.com/8422499/1738628

Android Custom ViewGroup and custom properties

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.