android自定義view的自定義屬性

在android自定義view中, 可以使用自定義的屬性來擴展功能。

原文

自定義屬性的步驟

1,定義屬性文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyTextViewAttr">
        <attr name="name" format="string" />
        <attr name="age" format="integer" />
        <attr name="married" format="boolean" />
        <attr name="sex" format="enum">
            <enum name="male" value="0" />
            <enum name="female" value="1" />
        </attr>
        <attr name="mycolor" format="color|reference" />
        <attr name="myheight" format="dimension" />
    </declare-styleable>
</resources>

其中,

declare-styleable.name是我們的自定義屬性的命名空間,代碼中使用會作爲前綴。

attr.name是屬性名。

attr.format是數據格式。

各個屬性格式含義如下表

format detail
boolean 布爾值
enum 枚舉
reference xml資源id
color 顏色
dimension 尺寸
flag 位或運算
float 浮點數
fraction 百分數
integer 整數
string 字符串

2,在xml中使用自定義屬性

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:myAttrs="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.qefee.pj.mytextview.MainActivity">

    <com.qefee.pj.mytextview.view.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        myAttrs:name = "tom"
        myAttrs:age = "20"
        myAttrs:married = "true"
        myAttrs:sex = "male"
        myAttrs:mycolor = "#66ccff"
        myAttrs:myheight = "60dp" />
</RelativeLayout>

xmlns:myAttrs這一行是引入我們的自定義屬性,myAttrs這個名字可以隨意,就是下面使用的自定義屬性的前綴。

myAttrs:name使用自定義屬性。myAttrs就是上面定義的前綴了,name是在自定義屬性名。

自定義屬性的數據格式參考1。

3, 在自定義view中使用自定義屬性

package com.qefee.pj.mytextview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

import com.qefee.pj.mytextview.R;

public class MyTextView extends TextView {

    /**
     * log tag for MyTextView
     */
    private static final String TAG = "MyTextView";

    String name;
    int age;
    boolean married;
    String sex;
    Drawable color;
    float height;


    public MyTextView(Context context) {
        super(context);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        showMyAttrs(context, attrs);
    }

    public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        showMyAttrs(context, attrs);
    }

    private void showMyAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextViewAttr);
        name = typedArray.getString(R.styleable.MyTextViewAttr_name);
        age = typedArray.getInt(R.styleable.MyTextViewAttr_age, 0);
        married = typedArray.getBoolean(R.styleable.MyTextViewAttr_married, false);
        sex = typedArray.getString(R.styleable.MyTextViewAttr_sex);
        color = typedArray.getDrawable(R.styleable.MyTextViewAttr_mycolor);
        height = typedArray.getDimension(R.styleable.MyTextViewAttr_myheight, 0f);

        typedArray.recycle();

        Log.i(TAG, "MyTextView: length = " + typedArray.length());
        Log.i(TAG, "MyTextView: name = " + name);
        Log.i(TAG, "MyTextView: age = " + age);
        Log.i(TAG, "MyTextView: married = " + married);
        Log.i(TAG, "MyTextView: sex = " + sex);
        Log.i(TAG, "MyTextView: color = " + color);
        Log.i(TAG, "MyTextView: height = " + height);
    }
}

MyTextViewAttr就是1中自定義屬性的命名空間。

MyTextViewAttr_name引用自定義屬性要加上前綴。

參考文章

發佈了130 篇原創文章 · 獲贊 38 · 訪問量 60萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章