Android custom control and android Control

Source: Internet
Author: User

Android custom control and android Control

Today, let's take a look at the introduction of Android custom controls. In Android, xml is generally written using a single control. However, in some projects, A single control is sometimes not satisfied, so we can customize the control. The advantage of using a custom control is that on the one hand it is more flexible, in the case of a large amount of data, the efficiency of the custom control is higher than that of the write layout file. To use this control elsewhere, you only need to reference the control.

The following describes a custom control and its precautions:

1. Create an xml file to write the layout you want to customize

2. Create an attrs. xml structure in res/values as follows:

<Resources>
<Declare-styleable name = "myshow">
<Attr name = "tvTitle" format = "string"/>
<Attr name = "titleMinLength" format = "integer"/>
<Attr name = "tvHint" format = "string"/>
<Attr name = "title" format = "color"/>
<Attr name = "tvValue" format = "string"/>
</Declare-styleable>
</Resources>


Describes the type and the specified type of the format attribute in each attr tag in detail.

1 "reference" // reference a resource ID

<Declare-styleable name = "name">
<Attr name = "background" format = "reference"/>
</Declare-styleable>

Usage:
<ImageView
Android: layout_width = "42dp"
Android: layout_height = "42dp"
Android: background = "@ drawable/image ID"
/>


2 "color" // color

<Declare-styleable name = "name">
<Attr name = "textcolor" format = "color"/>
</Declare-styleable>

Usage:

<TextView
Android: layout_width = "42dp"
Android: layout_height = "42dp"
Android: textcolor = "# ffffff"
/>


3 "boolean" // boolean Value

<Declare-styleable name = "name">
<Attr name = "focusable" format = "boolean"/>
</Declare-styleable>

Usage:

<Button
Android: layout_width = "42dp"
Android: layout_height = "42dp"
Android: focusable = "true"
/>

4 "dimension" // dimension value
<Declare-styleable name = "name">
<Attr name = "layout_width" format = "dimension"/>
</Declare-styleable>

Usage:

<Button
Android: layout_width = "42dp"
Android: layout_height = "42dp"
/>

5 "float" // floating point value

<Declare-styleable name = "AlphaAnimation">
<Attr name = "fromAlpha" format = "float"/>
<Attr name = "toAlpha" format = "float"/>
</Declare-styleable>

Usage:

<Alpha
Android: fromalphi = "1.0"
Android: toAlpha = "0.5"
/>

6 "integer" // integer value

<Declare-styleable name = "AnimatedRotateDrawable">
<Attr name = "visible"/>
<Attr name = "frameDuration" format = "integer"/>
<Attr name = "framesCount" format = "integer"/>
<Attr name = "pivotX"/>
<Attr name = "ty"/>
<Attr name = "drawable"/>
</Declare-styleable>

Usage:

<Animated-rotate
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: drawable = "@ drawable/image ID"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: framesCount = "12"
Android: frameDuration = "100"
/>

7 "string" // string

<Declare-styleable name = "AlphaAnimation">
<Attr name = "strName format =" string "/>
<Attr name = "strTitle" format = "string"/>
</Declare-styleable>

Usage:

<Com. google. android. maps. MapView
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: apiKey = "0jokq80od1jl9c6haja99ugxcri2cgjko"
/>

"Fraction" // percentage, such as 200%

<Declare-styleable name = "RotateDrawable">
<Attr name = "visible"/>
<Attr name = "fromDegrees" format = "float"/>
<Attr name = "toDegrees" format = "float"/>
<Attr name = "effectx" format = "fraction"/>
<Attr name = "ty" format = "fraction"/>
<Attr name = "drawable"/>
</Declare-styleable>

Usage:

<Rotate xmlns: android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ anim/animation ID"
Android: fromDegrees = "0"
Android: toDegrees = "360"
Android: Required Tx = "200%"
Android: Ty = "300%"
Android: duration= "5000"
Android: repeatMode = "restart"
Android: repeatCount = "infinite"
/>

9 "enum" // enumeration Value

<Declare-styleable name = "name">
<Attr name = "orientation">
<Enum name = "horizontal" value = "0"/>
<Enum name = "vertical" value = "1"/>
</Attr>
</Declare-styleable>

Usage:

<LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical \ horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>

</LinearLayout>


10

Note: Multiple Type values can be specified during attribute definition.

<Declare-styleable name = "name">
<Attr name = "background" format = "reference | color"/>
</Declare-styleable>
Usage:
<ImageView
Android: layout_width = "42dip"
Android: layout_height = "42dip"
Android: background = "@ drawable/image ID | # ffffff"
/>

3. Define a custom class to inherit a View. Which View to inherit depends on your actual situation?

4. reference the custom class to the xml you need

<1> the path of the current location must be referenced before addition, for example:

Xmlns: fx = "http://schemas.android.com/apk/res/fx.com.bitmapsolution"

<2> reference a specific control and obtain a property in the control.

<Fx.com. bitmapsolution. MyView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Fx: tvTitle = "@ string/title3"
Fx: titleMinLength = "4"
Android: layout_marginTop = "5dp"
/>

At this point, a simple custom control is completed. The custom control has time to write another custom control ~~









Question about android custom control development ???

You can set the id for RelativeLayout, and then set onclickListener in the java file. TextView can also do the same.
 
Android Custom Controls

Controls all belong to the View class
Visibility attributes: VISIBLE, INVISIBLE, and GONE
You can call the setVisibility method, for example
TextView txt;
Txt. setVisibility (View. GONE); // The control disappears!
Txt. setVisibility (View. VISIBLE); // The control appears again!

Alternatively, you can use a ViewStub control to dynamically load layout files.
It may not be suitable for your problem. How can I search it by myself?

Hope to help you
 

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.