关于如何在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);


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