- 首页 > it技术 > >
Android自定义顶部导航栏控件
class HeaderBar @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
//重写构造方法在java里面 我们一般是重写三个构造方法//在kotlin中 我们可以使用@JvmOverloads constructor(
//context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
//)后面的两个参数我们不传的可以使用的是默认值//定义一些变量private var isShowback = true
private var titleText: String? = null
private var rightText: String? = null
//初始化 在init方法中 初始化布局设置style
init {
//自定义属性val typedArray = context.obtainStyledAttributes(attrs, R.styleable.HeaderBar)
//取出在布局中定义的属性isShowback = typedArray.getBoolean(R.styleable.HeaderBar_isShowBack, true)
titleText = typedArray.getString(R.styleable.HeaderBar_titleText)
rightText = typedArray.getString(R.styleable.HeaderBar_rightText)
initView()
typedArray.recycle()
}
//初始化控件的方法private fun initView() {
//填充布局View.inflate(context, R.layout.layout_header_bar, this)
mLeftIv.visibility = if (isShowback) View.VISIBLE else View.INVISIBLE
titleText?.let {
mTitleTv.text = it
}
rightText?.let {
mRightTv.text = it
mRightTv.visibility = View.VISIBLE
}
mLeftIv.onClick {
if (context is Activity)
(context as Activity).finish()
}
}
fun getRightView(): TextView {
return mRightTv
}
【Android自定义顶部导航栏控件】}
推荐阅读