UI类

自定义的View在XML中引用处理

2016-12-09  本文已影响20人  程序狮

1、定义一个需要的自定义View的类

<code>
/** 内容标题 */
public class HomeTitle extends LinearLayout
</code>

2、首先,在values文件夹下定义一个atts.xml的文件,描述自定义的控件的属性

<code>
/**首页头部title */
<declare-styleable name="HomeTitle"> //自定义属性的命名空间
<attr name="htTitle" format="string" /> //定义属性类型与名称
</declare-styleable>
</code>

3、在xml中引用此控件

3.1、先声明要因为自定义控件的自定义属性

Paste_Image.png

3.2、使用自定义属性

Paste_Image.png

3.3、自定义代码中使用此属性

在构造方法中获取

<code>
TypedArray a = mCon.obtainStyledAttributes(attrs, R.styleable.TextViewLimitLine);
initLine = a.getInteger(0,3); //获取列表的第几个变量
a.recycle(); //记得回收
</code>

上一篇下一篇

猜你喜欢

热点阅读