【Android】创建自定义控件

来源:互联网 发布:中国拍卖行业协会网络 编辑:程序博客网 时间:2024/05/10 21:30

上一篇上说道引用布局文件 虽然方便了一些 但是每次引用了之后布局中的控件的单击事件的触发依然需要重新编写代码 添加了很多重复代码 这种情况下可以使用自定义控件来解决

还是以title为例

新疆爱你TitleLayout让它继承自LinearLayout

public class TitleLayout extends LinearLayout{

public TitleLayout(Context context, AttributeSet sttrs){

super(context, attrs);

LayoutInflater.from(context).inflate(R.layout.title, this);

}

}

然后只需要在布局文件中添加这个自定义控件就行了

<zn.ziggs.uicustomviews.TitleLayout

android:layout_width="match_parent"

android_height="wrap_content">

</cn.ziggs.uicustomviews.TitleLayout>

此时在TitleLayout中添加控件的触发事件就ok了

0 0