Android declare-styleable: Use of custom control attributes (ATTR. XML, typedarray)

Source: Internet
Author: User

Take launcher as an example to describe the attributes of a custom control:

1. Define controls in layout, for example:

<Com. junction. launcher. draglayer

Xmlns: Android= "Http://schemas.android.com/apk/res/android"

Xmlns: Launcher= "Http://schemas.android.com/apk/res/com.junction.launcher"

Android: Id = "@ + ID/drag_layer" Android: layout_width = "match_parent" Android: layout_height = "match_parent">

<Com. Android. launcher. Workspace

Android: Id = "@ + ID/workspace"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Launcher: defaultscreen = "1">


<Include Android: Id = "@ + ID/cell1" layout = "@ layout/workspace_screen"/>

<Include Android: Id = "@ + ID/cell2" layout = "@ layout/workspace_screen"/>

<Include Android: Id = "@ + ID/cell3" layout = "@ layout/workspace_screen"/>

</COM. Android. launcher. workspace>

2. There is an attribute under the workspace of the control:Launcher: defaultscreenThis attribute is not defined by viewgroup (workspace class is inherited from viewgroup), so weIt can be defined in attrs. XML in \ res \ values.

The specific definition method is as follows:

<Resources>

<Declare-styleable name = "workspace">

<ATTR name = "defaultscreen" format = "integer"/>

</Declare-styleable>

</Resources>

The reference method isDeclare xmlns: launcher first= "Http://schemas.android.com/apk/res/com.junction.launcher" (R. Java) so you can useLauncher: defaultscreen.

3. In this way, we can remove the value assigned to this attribute in layout in the workspace class. The method is as follows:

Int mdefaultscreen;

Typedarray A = context. obtainstyledattributes (attrs, R. styleable. workspace, defstyle, 0 );

Mdefaultscreen = A. getint (R. styleable. workspace_defaultscreen, 1 );

A. Recycle ();

++

Your own case: You can see a GIF player demo program.

Other case: http://blog.csdn.net/Android_Tutor/archive/2010/04/20/5508615.aspx

++

Reprinted from: http://blog.csdn.net/tinafhx/archive/2010/02/05/5290878.aspx

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.