關於Android attrs 自定義屬性的說明

  寫個自定義控件時常常要自定義一些本身的屬性,平時用的都是那幾個,今天就順便一塊兒總結一下這個東東吧~android

  1、定義:屬性的定義都在attrs.xml文件裏面;app

  2、讀取:經過都是經過TypedArray去讀取的,要獲取TypedArray都是經過context.obtainStyledAttributes去獲取的,它有幾個重載方法,通常形如: TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView);佈局

  3、使用:要使用自定義屬性,得先在佈局文件聲明 xmlns:app="http://schemas.android.com/apk/res-auto" 固然,你不喜歡app也能夠自定義名字,形如:xmlns:custom="http://schemas.android.com/apk/res/{packagename}" code

  4、自定義format的概覽:orm

format名稱 format類型
reference
表示引用,參考某一資源ID
string
表示字符串
color
表示顏色值
boolean
表示尺寸值
dimension
表示布爾值
float
表示浮點值
integer
表示整型值
fraction
表示百分數
enum
表示枚舉值
flag
表示位運算

 

  5、具體說明:xml

  5.1. reference:參考某一資源ID。three

    (1)屬性定義:圖片

            <declare-styleable name = "名稱">資源

                   <attr name = "cutom_id" format = "reference" />字符串

            </declare-styleable>

    (2)屬性使用:

             <CustomView

                     android:layout_width = "wrap_content"
                     android:layout_height = "wrap_content"
                     app:cutom_id = "@drawable/圖片ID"

                     />

  5.2. color:顏色值。

    (1)屬性定義:

            <declare-styleable name = "名稱">

                   <attr name = "custom_color" format = "color" />

            </declare-styleable>

    (2)屬性使用:

            <CustomView

                     android:layout_width = "wrap_content"
                     android:layout_height = "wrap_content"
                     app:custom_color = "#00FF00"

                     />

  5.3. boolean:布爾值。

    (1)屬性定義:

            <declare-styleable name = "名稱">

                   <attr name = "custom_b" format = "boolean" />

            </declare-styleable>

    (2)屬性使用:

            <CustomView

                    android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"

                    app:custom_b = "true"

                    />

  5.4. dimension:尺寸值。

    (1)屬性定義:

            <declare-styleable name = "名稱">

                   <attr name = "custom_width" format = "dimension" />

            </declare-styleable>

    (2)屬性使用:

            <CustomView

                    android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"

       app:custom_width="44dp"

                    />

  5.5. float:浮點值。

    (1)屬性定義:

            <declare-styleable name = "名稱">

                   <attr name = "custom_alpha" format = "float" />

            </declare-styleable>

    (2)屬性使用:

            <CustomView
              android:layout_width = "wrap_content"

                 android:layout_height = "wrap_content"

       app:custom_alpha="0.5"

                   />

  5.6. integer:整型值。

    (1)屬性定義:

            <declare-styleable name = "名稱">

                   <attr name = "custom_number" format="integer" />

            </declare-styleable>

    (2)屬性使用:

            <CustomView

       android:layout_width = "wrap_content"

                  android:layout_height = "wrap_content"

       app:custom_number="5"

                   />

  5.7. string:字符串。

    (1)屬性定義:

            <declare-styleable name = "名稱">
                   <attr name = "custom_key" format = "string" />
            </declare-styleable>

    (2)屬性使用:

            <CustomView
                    android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"
                    app:custom_key = "test_msg"

                    />

  5.8. fraction:百分數。

    (1)屬性定義:

            <declare-styleable name="名稱">
                   <attr name = "custom_percent" format = "fraction" />
            </declare-styleable>

    (2)屬性使用:

            <CustomView

                 android:layout_width = "wrap_content"
                 android:layout_height = "wrap_content"

                 app:custom_percent = "200%"

                   />

  5.9. enum:枚舉值。

    (1)屬性定義:

            <declare-styleable name="名稱">
                   <attr name="custom_orientation">
                          <enum name="horizontal" value="0" />
                          <enum name="vertical" value="1" />
                   </attr>            

            </declare-styleable>

    (2)屬性使用:

            <CustomView

                    android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"

                    app:custom_orientation = "vertical"
            />

  5.10. flag:位或運算。

     (1)屬性定義:

             <declare-styleable name="名稱">
                    <attr name="custom_mode">
                            <flag name = "mode_one" value = "0" />
                            <flag name = "mode_two" value = "1" />
                            <flag name = "mode_three" value = "2" />
                     </attr>         

             </declare-styleable>

     (2)屬性使用:

            

            <CustomView

                    android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"

                    app:custom_mode = "mode_one|mode_two|mode_three"
            />

     

  5.11 注意: 屬性定義時能夠指定多種類型值。

    (1)屬性定義:

            <declare-styleable name = "名稱">

                   <attr name = "custom_background" format = "reference|color" />

            </declare-styleable>

    (2)屬性使用:

             <CustomView

                     android:layout_width = "wrap_content"
                     android:layout_height = "wrap_content"
                     app:custom_background = "@drawable/圖片ID|#00FF00"

                     />

相關文章
相關標籤/搜索