前言:项目搭建UI中,我们总会遇到很多都是地方重复样式,比如:
现在来学习一下如何自定义一个UI模块,重复利用提高开发效率以及避免代码冗余。
自定义UI模块步骤:
一、先贴出一个案例:
1.自定义view类(ServiceView )
public class ServiceView extends LinearLayout {
private TextView tv_content; //右边显示的内容
private TextView tv_name; //条目title
private ImageView iv_pic; //左边图片
private ImageView iv_arrow; //向右的箭头
String mtitle, mcontent;
boolean mArrow; //右边箭头
int imgid;
//用代码new对象时
public ServiceView(Context context) {
super(context);
initView();
}
//有属性时
public ServiceView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ServiceView);
mtitle = ta.getString(R.styleable.ServiceView_mtitle);
mcontent = ta.getString(R.styleable.ServiceView_mcontent);
imgid = ta.getResourceId(R.styleable.ServiceView_mimg, imgid);
mArrow = ta.getBoolean(R.styleable.ServiceView_mright, true);
ta.recycle();
initView();
}
//有style样式时候
public ServiceView(Context context, AttributeSet attrs, int defStyleAttr) {