自定義View增長屬性第一步:定義屬性資源文件android
在/res/values 文件夾下創建「Values XML layout」,按照以下定義一個textview的屬性數組
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="name" format="reference"/> <attr name="sex" format="reference"/> <attr name="age" format="integer"/> <attr name="textColor" format="color"/> <attr name="textSize" format="dimension"/> <declare-styleable name="HsTextView"> <attr name="name"/> <attr name="sex"/> <attr name="age"/> <attr name="textColor"/> <attr name="textSize"/> </declare-styleable> </resources>
節點attr 是定義屬性用的,name 是屬性名稱 ,format 是屬性的類型,用<declare-styleable name="HsTextView">來給定義的屬性命名,這裏在layout裏面須要引用這個名字。app
另外除了reference與integer color dimension屬性之外還有以下屬性:eclipse
1. float:浮點值。函數
3. string:字符串工具
4. fraction:百分數。字體
5. enum:枚舉值spa
6. flag:是本身定義的,相似於 android:gravity="top",就是裏面對應了本身的屬性值。.net
8.reference|boolean:布爾值的資源文件code
dimension主要是sp與dp屬性,在獲取的時候須要轉換成像素值。reference是資源文件,在layout中設定值的時候須要用"@.."來引入資源文件。不能直接設定,設定完畢以後在layout中如此定義:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:hsTextViewattr="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.skymaster.hs.messagetest.HsTextView android:layout_width="wrap_content" android:layout_height="wrap_content" hsTextViewattr:name = "@string/name" hsTextViewattr:sex="@string/sex" /> </LinearLayout>
這裏要提示一點,xmlns:hsTextViewattr="http://schemas.android.com/apk/res-auto" 這裏是指定命名空間,xmlns:XML namespace, 指定的名字是hsTextViewattr,後面要使用這個屬性的時候就用「hsTextViewattr:name = "@string/name"」,系統的命名空間是android因此都是"android:layout_width...",有一點須要說明的是在eclipse中這個命名空間的寫法是:
xmlns:你本身定義的名稱=http://schemas.android.com/apk/res/你程序的package包名
而在android studio中只要改爲"res-auto"這樣就方便不少了
自定義View增長屬性第二步:在構造函數裏獲取屬性
定義一個本身的View而且繼承TextView,重寫構造方法獲取自定義的屬性:
public class HsTextView extends TextView { private static final String TAG = "HsTextView"; private final String DEFAULT_TEXT_NAME = "默認姓名"; private final int DEFAULT_TEXT_AGE = 100; private final String DEFAULT_TEXT_SEX = "男"; private final int DEFAULT_TEXT_SIZE = 10;//sp private final int DEFAULT_TEXT_COLOR = 0xff00c0c0; private String mTextName = DEFAULT_TEXT_NAME; private int mTextAge = DEFAULT_TEXT_AGE; private String mTextSex = DEFAULT_TEXT_SEX; private int mTextColor = DEFAULT_TEXT_COLOR; private float mTextSize = sp2px(DEFAULT_TEXT_SIZE); public HsTextView(Context context, AttributeSet attrs) { super(context, attrs); obtainStyleAttrs(attrs); } private void obtainStyleAttrs(AttributeSet attrs) { TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.HsTextView); mTextName = a.getString(R.styleable.HsTextView_name); mTextSex = a.getString(R.styleable.HsTextView_sex); mTextAge = a.getInteger(R.styleable.HsTextView_age,mTextAge); mTextColor = a.getColor(R.styleable.HsTextView_textColor,mTextColor); mTextSize = (int) a.getDimension(R.styleable.HsTextView_textSize,mTextSize); setTextColor(mTextColor); setTextSize(mTextSize); setText("姓名:"+mTextName+"\n"+"年齡:"+mTextAge+"\n"+"性別:"+mTextSex); a.recycle(); } public HsTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } private float sp2px(int spVal){ return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spVal, getResources().getDisplayMetrics()); } }
經過xml實現空間對象默認調用的是2個參數的構造函數也就是 public HsTextView(Context context, AttributeSet attrs)
這裏傳入了上下文與AttribueSet,從字面理解就是屬性集合,經過Context的obtainStyledAttributes(..)獲取屬性數組,記得這裏要定義的TypeArray使用完以後必定要釋放a.recycle();另一個須要注意的是定義字體大小定義的屬性是sp須要轉換成像素
因此要使用工具類實現:
sp2px: TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spVal,getResources().getDisplayMetrics());基本是固定寫法。另外dp轉px的寫法:
dp2px:TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpVal, getResources().getDisplayMetrics());
這裏返回的都是float,根據須要進行類型強制轉換。
完成以後就能夠顯示了本身定義的屬性了:
引用博客地址http://blog.csdn.net/vipzjyno1/article/details/23696537