【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. 细节注意

  1. Android的自定义View预览,需要在自定义View编写无误且编译过,才能直接在PreView窗口预览效果
  2. 建议属性名前面加前缀,如:row_xxxxx;规范 & 避免资源命名冲突
  3. 属性值的获取,注意使用对应的TypeArray方法获取,否则会坑了自己。
  4. 属性值对应的获取方法可参考

转载地址:https://cupster.blog.csdn.net/article/details/112171415 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:【5年Android从零复盘系列之八】Android自定义View(3):衍生/扩展式 详解(图文)
下一篇:【5年Android从零复盘系列之六】Android自定义View(1):基础详解(图文)

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月04日 23时01分47秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章