【5年Android从零复盘系列之七】Android自定义View(2):组合式详解(图文)
发布日期:2021-06-29 18:17:37
浏览次数:3
分类:技术文章
本文共 3086 字,大约阅读时间需要 10 分钟。
1.简述
顾名思义,即xml文件中根布局使用RelativeLayout(同理其他布局控件),
内部使用其他View控件,布局摆放,组合成XxxView的原始布局。 然后,Java类继承根布局标签对应的容器类,覆写构造函数,并初始化各子View的变量。 是最常用、最稳妥、不易产生过度绘制、内存泄漏等问题。2. 实践
2.1 编辑 组合View的UI
2.2 res/values/attrs.xml 编写快捷属性
2.3 继承根标签布局类,获取属性、设置属性
package com.cupster.base_super_resource;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Color;import android.graphics.drawable.Drawable;import android.os.Build;import android.text.TextUtils;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TextView;import androidx.annotation.RequiresApi;public class CommonRowEntranceView extends RelativeLayout { public CommonRowEntranceView(Context context) { super(context); initInnerView(context); } public CommonRowEntranceView(Context context, AttributeSet attrs) { super(context, attrs); initInnerView(context); initAttrs(context, attrs); } public CommonRowEntranceView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initInnerView(context); initAttrs(context, attrs); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public CommonRowEntranceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); initInnerView(context); initAttrs(context, attrs); } RelativeLayout mRootLayout ; TextView mTitle ; ImageView mArrow; private void initInnerView(Context context) { LayoutInflater.from(context).inflate(R.layout.view_common_row_entrance, this, true);//此处应为关联true,否则报错 mRootLayout = findViewById(R.id.layout_root); mArrow = findViewById(R.id.icon_arrow); mTitle = findViewById(R.id.title); } private void initAttrs(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonRowEntranceView); //取值 Drawable bgDrawable = typedArray.getDrawable(R.styleable.CommonRowEntranceView_row_bg); int txtColor= typedArray.getInt(R.styleable.CommonRowEntranceView_row_txt_color, Color.parseColor("#808080")); String title = typedArray.getString(R.styleable.CommonRowEntranceView_row_txt); //使用 if (bgDrawable == null) { mRootLayout.setBackgroundColor(Color.WHITE); } else { mRootLayout.setBackground(bgDrawable); } if (!TextUtils.isEmpty(title)) { mTitle.setText(title); } mTitle.setTextColor(txtColor); //释放资源 typedArray.recycle(); }}
2.4 实际场景使用
3. 细节注意
- Android的自定义View预览,需要在自定义View编写无误且编译过,才能直接在PreView窗口预览效果
- 建议属性名前面加前缀,如:row_xxxxx;规范 & 避免资源命名冲突
- 属性值的获取,注意使用对应的TypeArray方法获取,否则会坑了自己。
- 属性值对应的获取方法可参考
转载地址:https://cupster.blog.csdn.net/article/details/112171415 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月18日 16时11分11秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
指针数组和数组指针的一些总结
2019-04-30
在init.rc中增加开机启动服务
2019-04-30
【源码分享】python开发的高通平台fastboot刷机工具
2019-04-30
C语言中计算结构体大小的方法总结
2019-04-30
Qt之Excel:QAxObject 自定义封装类 ExcelManager
2019-04-30
Qt之 QTableWidget 实现会计手账
2019-04-30
Qt之菜单:工具条弹出式菜单
2019-04-30
Python 01-安装
2019-04-30
Python 02-基础语法
2019-04-30
Python 04-基本运算
2019-04-30
Python 05-字符串
2019-04-30
Python 06-序列
2019-04-30
Python 07-集合和字典
2019-04-30
Python 08-文件读写
2019-04-30
Python 09-目录操作
2019-04-30
Python 10-日期时间
2019-04-30
sqlite3 Python模块
2019-04-30
Python 库-openpyxl 1:简介
2019-04-30
Python 库-openpyxl 2:使用教程
2019-04-30
[技术发展-8]:高级研修班-人工智能篇-数字化时代下的大数据与人工智能
2019-04-30