Android自定义组合View简单示例,美团架构师深入讲解Android开发

android:text=“标题”
android:textColor="@color/white"
android:textSize=“18sp”
android:textStyle=“bold”
android:gravity=“center”/>

这里根布局是merge,不是通常的LinearLayout或其他ViewGroup,因为你所自定义的组合view就是一个ViewGroup,在xml中根布局能减少还是要减少的,少了一层嵌套,它不香吗,至于你需要在根布局设置的属性如水平垂直或重心等,你可以在你自定义的View中设置,请往下看

二、new 你需要的组合view 重写构造函数

class Custom

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

开源分享完整内容戳这里

ViewGroup: LinearLayout {

constructor(context: Context): this(context, null)//注意this()这调用了第二个构造函数
constructor(context: Context, attrs: AttributeSet?): this(context, attrs, 0)//注意this()这调用了第三个构造函数
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr) {}
}

三、编辑你需要的组合view的自定义属性

在res/values文件夹下找到attrs.xml,如果没有attrs.xml,右键values -> new -> Values Resource File,命名attrs即可,在attrs.xml文件中添加 declare-styleable

<?xml version="1.0" encoding="utf-8"?> // CustomViewGroup这个名字和第二步新建的view名字一致的 // 你可以加其他属性都可以,format支持很多类型的,比如color,drawable等你可以自己看一看

四、开始在你的自定义组合view中写你的代码

class CustomViewGroup: LinearLayout {

lateinit var title: TextView
lateinit var operation: TextView

constructor(context: Context): this(context, null)
constructor(context: Context, attrs: AttributeSet?): this(context, attrs, 0)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr) {
//因为我们引入的布局文件根布局是merge,而我们的CustomViewGroup又是继承LinearLayout,所以我们在这里设置几个属性
this.orientation = HORIZONTAL
this.gravity = Gravity.CENTER_VERTICAL
this.setBackgroundColor(ContextCompat.getColor(context, R.color.colorAccent))
init(context, attrs)
}

private fun init(context: Context, attrs: AttributeSet?) {
//加载你的组合布局文件到这个自定义的CustomViewGroup内
val inflate = LayoutInflater.from(context).inflate(R.layout.view_custom_viewgroup, this)
//利用findViewById找到你布局文件中的控件
val ivBack: ImageView = inflate.findViewById(R.id.iv_back)
//如返回键无特殊操作,可以直接finish(),如有其它操作可自定义其它操作方法
ivBack.setOnClickListener {(context as Activity).finish()}

title = inflate.findViewById(R.id.tv_title)
operation = inflate.findViewById(R.id.tv_operation)

//获取可配置的属性,设置给控件
val attr = context.obtainStyledAttributes(attrs, R.styleable.CustomViewGroup)
title.text = attr.getString(R.styleable.CustomViewGroup_title)
operation.text = attr.getString(R.styleable.CustomViewGroup_operation)

//用完要回收
attr.recycle()
}

/* 可以对外提供设置text方法 */
fun setTitle(s: String){
title.text = s
}

fun setOperation(s: String){
operation.text = s
}

//设置对右文本的点击监听
fun setOnOperationClickListener(listener: OnOperationClickListener){
operation.setOnClickListener {
listener.onClick()
}
}
}

interface OnOperationClickListener{
fun onClick()
}

使用

xml中

<?xml version="1.0" encoding="utf-8"?>

<com.limeng.thecreator.view.CustomViewGroup
android:id="@+id/cvg_title"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
app:title=“我是标题”
app:operation=“筛选”/>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值