關於如何在XML中使用自定義控件。

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);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章