加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

关于如何在XML中使用自定义控件。

发布时间:2020-12-15 22:57:10 所属栏目:百科 来源:网络整理
导读:1.首先创建一个类继承View或者是View的子类,根据你需要的类型进创建。 2.添加一个带两个参数的构造方法。 3. 定义自定义控件属性。如: 参照ApiDemosresvaluesattrs.xml定义属性 declare-styleable name="VerticalTextView" attr name="content" format=

1.首先创建一个类继承View或者是View的子类,根据你需要的类型进创建。

2.添加一个带两个参数的构造方法。

3. 定义自定义控件属性。如:

参照ApiDemosresvaluesattrs.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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读