android:textStyle=“bold”
android:gravity=“center”/>
这里根布局是merge,不是通常的LinearLayout或其他ViewGroup,因为你所自定义的组合view就是一个ViewGroup,在xml中根布局能减少还是要减少的,少了一层嵌套,它不香吗,至于你需要在根布局设置的属性如水平垂直或重心等,你可以在你自定义的View中设置,请往下看
二、new 你需要的组合view 重写构造函数
class CustomViewGroup: 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=“筛选”/>
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!](https://bbs.csdn.net/topics/618165277)
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!