android自定义view复用UI

前言:项目搭建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) {
        
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值