前言:项目搭建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(Co

本文介绍了在Android开发中如何通过自定义View来复用UI,以提高开发效率并减少代码冗余。文章提供了一个自定义ServiceView的案例,包括创建自定义view类、布局文件、自定义属性的步骤,并鼓励开发者养成封装和复用的习惯。
最低0.47元/天 解锁文章
1783

被折叠的 条评论
为什么被折叠?



