自定义属性

自定义属性
布局文件
< com .example.administrator.myapplication.numberAddSubView
    android :id= "@+id/numberAddView"
    whq :number_background= "@drawable/add_sub_number_view_bg"
    whq :minValue= "1"
    whq :maxValue= "10"
    whq :value= "2"
    android :layout_width= "wrap_content"
    android :layout_height= "wrap_content" />

values包下创建attrs.xml文件

<? xml version= "1.0"  encoding= "utf-8" ?>
< resources>
    < declare-styleable  name= "numberAddSubView">//view的名称
        < attr  name= "value"  format= "integer|reference"/>
        < attr  name= "minValue"  format= "integer|reference"/>
        < attr  name= "maxValue"  format= "integer|reference"/>
        < attr  name= "number_background"  format= "reference"/>//引用类型
    </ declare-styleable>
</resources>

可以在activity代码中设置value 等的值//前提是有set方法
numberAddView = (numberAddSubView)findViewById(R.id. numberAddView);
numberAddView.setValue(15);

自定义属性并获取属性值
咱们自定义的属性都在attrs中
public numberAddSubView(Context context, AttributeSet attrs,  int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mContext = context;
    if(attrs!=null) {
这个是V7报中的 也可用TypedArray                              这里是context. obtainStyledAttributes 方法也可
        TintTypedArray typedArray=TintTypedArray.obtainStyledAttributes(context,attrs,R.styleable.numberAddSubView);
        int value=typedArray.getInt(R.styleable. numberAddSubView_value, 0); // 默认为 0
        if(value> 0) { // 不等于 0 才有值
            setValue(value); // 属性如果被赋值,则先执行属性,再执行构造函数,然后是未被赋值的属性初始化
        }
        int minValue=typedArray.getInt(R.styleable. numberAddSubView_minValue, 0); // 默认为 0
        if(minValue> 0) { // 不等于 0 才有值
            setMinValue(value); // 属性如果被赋值,则先执行属性,再执行构造函数,然后是未被赋值的属性初始化
        }
        int maxValue=typedArray.getInt(R.styleable. numberAddSubView_maxValue, 0); // 默认为 0
        if(maxValue> 0) { // 不等于 0 才有值
            setMaxValue(value); // 属性如果被赋值,则先执行属性,再执行构造函数,然后是未被赋值的属性初始化
        }
        Drawable  numberAddSubView_number_number_background = typedArray.getDrawable(R.styleable. numberAddSubView_number_background);

        if( numberAddSubView_number_number_background!= null) {
            if (Build.VERSION. SDK_INT >= Build.VERSION_CODES. JELLY_BEAN) {
                setBackground( numberAddSubView_number_number_background);
            }
        }
    }
    // 把布局添加到类中
    View view = View. inflate(context, R.layout. add_sub_number_view, numberAddSubView. this);
    btn_sub = (Button) findViewById(R.id. btn_sub);
    tv_value = (TextView) findViewById(R.id. tv_value);
    btn_add = (Button) findViewById(R.id. btn_add);

    btn_sub.setOnClickListener( this);
    btn_add.setOnClickListener( this);
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值