Use of custom properties in the android development tutorial

  • 2020-06-01 10:57:32
  • OfStack

Custom controls are often used in recent projects, so custom properties are often used. Here is the usage of custom properties:

The custom properties all exist in the /value/ attr.xml file in the following format.


<resource>
<declare-styleable name=" Customize the property name ">
<attr name=" The attribute name " format=" Attribute types "/>
......
</declare-styleable>
</resource>

The value of format in the custom property and its meaning are as follows:

format attribute values: reference, color, boolean, dimension, float, integer, string, fraction, enum, flag

1. reference: refer to ID.

(1) property definition:


<declare-styleable name = " The name of the ">
   <attr name = "background" format = "reference" />
</declare-styleable>

(2) property use:


<ImageView
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/ The picture ID"
/>

2.color: color value.

(1) property definition:


<declare-styleablename=" The name of the ">
<attrname="textColor"format="color"/>
</declare-styleable>

(2) property use:


<TextView
android:layout_width="42dip"
android:layout_height="42dip"
android:textColor="#00FF00"
/>

3.boolean: Boolean value.

(1) property definition:


<declare-styleablename=" The name of the ">
<attrname="focusable"format="boolean"/>
</declare-styleable>

(2) property use:


<Button
android:layout_width="42dip"
android:layout_height="42dip"
android:focusable="true"
/>

4.dimension: dimensions.

(1) property definition:


<declare-styleablename=" The name of the ">
<attrname="layout_width"format="dimension"/>
</declare-styleable>

(2) property use:


<Button
android:layout_width="42dip"
android:layout_height="42dip"
/>

5.float: floating point value.

(1) property definition:


<declare-styleablename="AlphaAnimation">
<attrname="fromAlpha"format="float"/>
<attrname="toAlpha"format="float"/>
</declare-styleable>

(2) property use:


<alpha
android:fromAlpha="1.0"
android:toAlpha="0.7"
/>

6.integer: integer value.

(1) property definition:


<declare-styleablename="AnimatedRotateDrawable">
<attrname="visible"/>
<attrname="frameDuration"format="integer"/>
<attrname="framesCount"format="integer"/>
<attrname="pivotX"/>
<attrname="pivotY"/>
<attrname="drawable"/>
</declare-styleable>

(2) property use:


<animated-rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ The picture ID"
android:pivotX="50%"
android:pivotY="50%"
android:framesCount="12"
android:frameDuration="100"
/>

7.string: string.

(1) property definition:


<declare-styleablename="MapView">
<attrname="apiKey"format="string"/>
</declare-styleable>

(2) property use:


<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
/>

8.fraction: percentage.

(1) property definition:


<declare-styleablename="RotateDrawable">
<attrname="visible"/>
<attrname="fromDegrees"format="float"/>
<attrname="toDegrees"format="float"/>
<attrname="pivotX"format="fraction"/>
<attrname="pivotY"format="fraction"/>
<attrname="drawable"/>
</declare-styleable>

(2) property use:


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

9.enum: enumeration value.

(1) property definition:


<declare-styleablename=" The name of the ">
<attrname="orientation">
<enumname="horizontal"value="0"/>
<enumname="vertical"value="1"/>
</attr>
</declare-styleable>

(2) property use:


<declare-styleable name = " The name of the ">
   <attr name = "background" format = "reference" />
</declare-styleable>
8

10.flag: bit or operation.

(1) property definition:


<declare-styleable name = " The name of the ">
   <attr name = "background" format = "reference" />
</declare-styleable>
9

(2) property use:


<ImageView
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/ The picture ID"
/>
0

Special note:

You can specify multiple type values when defining a property.

(1) property definition:


<declare-styleablename=" The name of the ">
<attrname="background"format="reference|color"/>
</declare-styleable>

(2) property use:


<ImageView
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/ The picture ID|#00FF00"
/>

Here's what AttributeSet and TypedArray do in custom controls:

The purpose of AttributeSet is to parse the properties (keyeg: background) and the values (valueeg: @drawable /icon) of the control in the layout file into AttributeSet and pass them to the constructor of the control (View) as it initializes. For attributes that are not native to Android, they are not recognized when handled in the View class, so we need to parse them ourselves. So this is going to use another class, TypedArray. In AttributeSet we have property names, we have property values, but how does the control know which property stands for what? This is done by TypedArray. The TypedArray object encapsulates the type information of each property defined in styleable in /values/ attrs.xml. Through TypedArray, we can know exactly what the values encapsulated in AttributeSet are, so we can apply these data.

AttributeSet is equivalent to a box of sugar, and TypedArray is equivalent to the label on the box of sugar, telling the user the taste of each sugar and so on. The taste of the box is determined by the contents of the user's styleable file.


Related articles: