首先是构造函数的写法
constructor(context: Context) : this(context, null)
constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int):super(context,attributeSet,defStyleAttr){
init(context,attributeSet)
}
LayoutInflater.inflate的注意事项
fun init(context: Context, attributeSet: AttributeSet?){
Log.i(TAG,"myCustomEditText z自定义---------")
view = LayoutInflater.from(context).inflate(R.layout.layout_mycustom_et,this,true)
txtDes = view?.findViewById(R.id.tv_des_et)
txtDesUnit = view?.findViewById(R.id.tv_unit)
et_num = view?.findViewById(R.id.et_num)
var a = context.obtainStyledAttributes(attributeSet,R.styleable.mycustom_et)
//获取是否要显示单位
visiblity = a!!.getBoolean(R.styleable.mycustom_et_txt_unit,true)
str = a!!.getString(R.styleable.mycustom_et_txt)
desstr = a!!.getString(R.styleable.mycustom_et_txt_des)
var desunit = a!!.getString(R.styleable.mycustom_et_txt_unit_des)
// var width = a!!.getString(R.styleable.mycustom_et_view_width)
// var height = a!!.getString(R.styleable.mycustom_et_view_height)
Log.i(TAG,"txtDes=$desstr")
Log.i(TAG,"str=$str")
Log.i(TAG,"visiblity=$visiblity")
when(visiblity){
true->{
txtDesUnit?.visibility = View.VISIBLE
txtDesUnit?.text = desunit
}
else-> txtDesUnit?.visibility = View.GONE
}
txtDes?.text = desstr
et_num?.text = Editable.Factory.getInstance().newEditable(str)
a.recycle()
}
- 其中
view = LayoutInflater.from(context).inflate(R.layout.layout_mycustom_et,this,true) this不能丢,他表示当前的MyCustomEditText class MyCustomEditText : LinearLayout
- 最后一个参数是true,表示父控件把子控件布局放入进来,同理知道,如果将true去掉改为false,那么父控件就没法和子控件布局连接起来。
组合控件改怎么设置自定义大小 ??????