【5年Android从零复盘系列之七】Android自定义View(2):组合式详解(图文)
发布日期:2021-06-29 18:17:37
浏览次数:2
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月04日 23时01分47秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
图像二值化----otsu(最大类间方差法、大津算法)(二)
2019-04-29
OpenCV编程案例:使用轮廓函数检测连通区域
2019-04-29
opencv使用cvFindContours提取联通域
2019-04-29
C++中MessageBox的常见用法
2019-04-29
ordfilt2函数功能说明
2019-04-29
在图像变换中用最小二乘法求解仿射变换参数
2019-04-29
软件包应用分享|基于RT-Thread的百度语音识别(一)
2019-04-29
12月8日 RCEA - RT-Thread能力认证考试考前通知
2019-04-29
论坛热贴 | RT-Thread音频驱动开发(一)
2019-04-29
基于 Keil MDK 移植 RT-Thread Nano
2019-04-29
【报名截至今晚】12月14日深圳嵌入式与音频开发专题会议预告
2019-04-29
移植 RT-Thread Nano 到 RISC-V
2019-04-29
软件包应用分享|基于RT-Thread的百度语音识别(二)
2019-04-29
在 RT-Thread Nano 上添加控制台与 FinSH
2019-04-29
一站式开发工具:RT-Thread Studio 正式发布
2019-04-29
留言有礼|谢谢你悄悄点了小星星,让我们跃居GitHub RTOS Star榜第一
2019-04-29
功能更新!C 函数也能在 MicroPython 中被调用啦
2019-04-29
东软载波携ES32+RT-Thread走进海尔集团
2019-04-29
今晚8点直播预告:RT-Thread Studio等相关主题答疑
2019-04-29