下面一段代码给大家介绍了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 = trueprivate var titleText: String? = nullprivate var rightText: String? = null//初始化 在init方法中 初始化布局 设置styleinit {//自定义属性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.INVISIBLEtitleText?.let {mTitleTv.text = it}rightText?.let {mRightTv.text = itmRightTv.visibility = View.VISIBLE}mLeftIv.onClick {if (context is Activity)(context as Activity).finish()}}fun getRightView(): TextView {return mRightTv}}总结
以上所述是小编给大家介绍的Android自定义顶部导航栏控件实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!