1.首先創建一個類繼承View或者是View的子類,根據你需要的類型進創建。
2.添加一個帶兩個參數的構造方法。
3. 定義自定義控件屬性。如:
參照ApiDemos\res\values\attrs.xml定義屬性
<declare-styleable name="VerticalTextView">
<attr name="content" format="string|reference" />
<attr name="contentColor" format="color|reference" />
<attr name="contentSize" format="dimension|reference" />
</declare-styleable>
4.然後在構造方法中讀出你需要的內容如:
TypedArray ta = context.obtainStyledAttributes(
attrs, R.styleable.VerticalTextView);
mContent = ta.getString(
R.styleable.VerticalTextView_content);