class Header @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
defStyleAttr: Int = 0
) : RelativeLayout(context, attributeSet, defStyleAttr) {
private val binding: HeaderBinding by lazy {
HeaderBinding.inflate(LayoutInflater.from(context), this, false)
}
init {
binding.ivBack.setOnClickListener { (context as Activity).finish() }
if (attributeSet != null) {
context.theme.obtainStyledAttributes(
attributeSet,
R.styleable.header, 0, 0
).apply {
binding.tvTitle.text = getString(R.styleable.header_header_title)
binding.ivBack.visibility = if (getBoolean(
R.styleable.header_header_showBack,
true
)
) View.VISIBLE else View.GONE
}
}
addView(binding.root)
}
fun setTitle(title:String){
binding.tvTitle.text=title
}
}
android kotlin自定义view写法
最新推荐文章于 2024-05-15 10:12:19 发布