Android custom control steps

Why should custom controls be step-by-step? Because many novice students, according to the requirements of the product to do some controls, do not know how to start.

After you have the steps, you can follow the routine, and you don’t have to be afraid anymore.

Determine the control type

First of all we have to judge this control is a custom control in which type, if you do not know the classification, students can see this article custom control classification

Gets the associated properties

This is actually called custom properties in custom controls. What are attributes?

Let’s look at the original properties of android controls:

Take our LinearLayout

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="41px"
        android:orientation="horizontal">
</LinearLayout>
Copy the code

There are origintation,layout_width,layout_height…. The origintation is unique to the LinearLayout, while the others are all present in the View

So when we write our own controls, some of the values are also configured using properties in the XML, so we need to have custom properties.

How do you customize properties in custom controls?

Custom property steps:

  1. Declaration attributes

In the attrs. XML file in the values directory of res (without creating a new one yourself), use the declare-styleable tag to customize the attributes.

In attrs. XML, it can be declared as resourse, for example:

<?xml version="1.0" encoding="utf-8"? >
<resources>
    <! -- Attribute Set -->
    <declare-styleable name="SearchView" >
        <! --hint-->
        <attr name="hint" format="string"/>
        <! Typedarray.getresourceid Specifies the resource ID.
        <attr name="search_ico" format="reference" />
        <! Clear the icon -->
        <attr name="clear_ico" format="reference" />
        <! -- Voice icon -->
        <attr name="voice_ico" format="reference" />
        <! -- Input type -->
        <attr name="input_type">
            <! Typearray. getInt --> typeArray.getInt --> typearray. getInt
            <enum name="text" value="0"/>
            <enum name="passwordText" value="1"/>
        </attr>

        <attr name="gravity">
            <! - different from enum enum can only choose one flag can choose more, such as: left | top - >
            <flag name="left" value="0" />
            <flag name="top" value="1" />
            <flag name="center" value="2" />
            <flag name="right" value="3" />
            <flag name="bottom" value="4" />
        </attr>
    </declare-styleable>

</resources>
Copy the code

The declaration-styleable tag represents a collection of properties, and the name property must have the same name as the custom control. Attr is used for each attribute, name is the attribute value, and format is the specified attribute type. Currently, 11 types are supported:

  • Reference: references resources

  • String: a string

  • Color: Color

  • Boolean: Indicates a Boolean value

  • Dimension: indicates the size value

  • Float: indicates the floating point

  • Integer: integer

  • A fraction

  • Enum: Enumeration type

  • Flag: bit or operation

  1. Used in layout files

First you need to add a namespace XMLNS: app = “http://schemas.android.com/apk/res-auto” then set properties directly using the namespace

<com.searchview.SearchView
      android:id="@+id/searchView"
      android:layout_width="match_parent"
      android:layout_height="50dp"
      android:layout_marginEnd="8dp"
      android:layout_marginStart="8dp"
      android:layout_marginTop="8dp"
      app:hint="aaa"
      app:search_ico="@drawable/ic_search"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />
Copy the code

measurement

Measurement is important to master, if the ViewGroup is measured (set its own size, because the ViewGroup can also be a child view) and set the expected child size.

If it’s a View, you need to measure yourself (set your size)

These two parameters need to be understood

The nature of custom controls