View 自定义 - 属性 Attribute

一、概念 

在 xml 中为控件设置的属性。自定义属性名称如果使用系统已定义的,例如 textSize 会在编译时报错。

格式类型定义/使用

string 字符串

<attr name = "myContent" format = "color" />

android:myContent = "Hello Word!"

color 颜色

<attr name = "myTextColor" format = "color" />

android:myTextColor = "#00FF00"

dimension 尺寸

<attr name = "myTextSize" format = "dimension" />

android:myTextSize = "12.sp"

reference 资源

<attr name = "myBackground" format = "reference" />

android:myBackground = "@drawable/图片ID"

boolean 布尔

<attr name = "myEnable" format = "boolean" />

android:myEnable = "true"

float 浮点

<attr name = "myAlpha" format = "float" />

android:myAlpha = "0.5F"

integer 整型

<attr name = "myMaxLines" format = "integer" />

android:myMaxLines = "3"

fraction 百分比

<attr name = "myOffset" format = "fraction" />

android:myOffset = "10%"

enum 枚举

<attr name = "myOrientation">

        <enum name = "horizontal" value="0" />

        <enum name = "vertical" value="1" />

</attr>

android:myOrientation = "vertical"

flag 位运算

位运算类型的属性在使用的过程中可以使用多个值

<attr name = "myGravity" />

        <flag nema="top" value="0x01">

        <flag nema="left" value="0x02">

        <flag nema="center_vertical" value="0x02">

</attr>

android:myGravity = "top|left"

混合类型

属性定义时可以指定多种类型值

<attr name = "myBackground" format = "reference|color" />

android:myBackground = "@drawable/图片ID"

android:myBackground = "#00FF00"

二、自定义步骤

2.1 创建资源文件(属性声明)

右键 values 目录 -> New File文件 -> 一般取名attrs.xml。

<resources><!--name使用自定义View的名称--><declare-styleable name="MyView"><!--name属性名称,format格式--><attr name="myText" format="string" /><attr name="myTextColor" format="color" /><attr name="myTextSize" format="dimension" /><attr name="myMaxLength" format="integer" /><attr name="myBackground" format="reference|color" /><!--枚举--><attr name="myInputType"><enum name="number" value="1"/><enum name="text" value="2"/></attr></declare-styleable>
</resources>

2.2 构造函数中配置

constructor(context: Context?) : super(context)

重写一个参数的构造函数,使用场景:代码 new 创建实例的时候调用。

constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

重写两个参数的构造函数,使用场景:xml中使用时调用(xml转java代码的时候反射)。

constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

重写三个参数的构造函数,使用场景:使用主题Style的时候调用。

class MyView : View {private var text: String? = nullprivate var textSize: Int? = nullprivate var textColor: Int? = nullprivate var maxLength: Int? = nullprivate var background: Int? = nullprivate var inputType: Int? = null//改成this调用2个参数的构造constructor(context: Context?) : this(context, null)//改成this调用3个参数的构造constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)//在这里统一进行处理constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {context?.let {//返回一个与attrs中列举出的属性相关的数组,数组里面的值由样式属性指定val attributes = it.obtainStyledAttributes(attrs, R.styleable.MyView)//获取自定义属性(格式:属性名称_条目名称)text = attributes.getString(R.styleable.MyView_myText)textSize = attributes.getDimensionPixelSize(R.styleable.MyView_myTextSize, 0)textColor = attributes.getColor(R.styleable.MyView_myTextColor, Color.BLACK)maxLength = attributes.getInt(R.styleable.MyView_myMaxLength,1)background = attributes.getResourceId(R.styleable.MyView_myBackground,R.drawable.ic_launcher_foreground)inputType = attributes.getInt(R.styleable.MyView_myInputType,0)//回收资源attributes.recycle()}}
}

2.3 布局中使用(属性使用)

  • 根布局添加命名空间(只需要输入app,IDE会自动补全)。
  • 控件名称使用完整路径(只需要输入自定义View的类名,IDE会自动补全)。
  • 未自定义的属性View会去处理(继承自View),使用自定义的属性就是 app: 开头的。
<!--根布局添加命名空间-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!--控件名称使用完整路径--><com.example.kotlindemo.view.MyViewandroid:layout_width="match_parent"android:layout_height="wrap_content"app:myBackground="@drawable/ic_launcher_foreground"app:myInputType="number"app:myText="Hello Word!"app:myTextColor="@color/black"app:myTextSize="20sp"app:myMaxLength="20"/>
</LinearLayout>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/137416.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

FPGA配置采集AR0135工业相机,提供2套工程源码和技术支持

目录 1、前言免责声明 2、AR0135工业相机简介3、我这里已有的 FPGA 图像处理解决方案4、设计思路框架AR0135配置和采集图像缓存视频输出 5、vivado工程1–>Kintex7开发板工程6、vivado工程1–>Zynq7100开发板工程7、上板调试验证8、福利&#xff1a;工程代码的获取 1、前…

Java多线程3种中断方式和终止方式

一、线程中断 Java 中有以3 种方法可以中断正在运行的线程&#xff1a; 使用&#xff08;volatile修饰&#xff09;退出标志&#xff0c;使线程正常退出&#xff0c;也就是当 run() 方法完成后线程中止&#xff1b;也可使用 Atomic 变量作为退出标志&#xff0c;同样可以实现…

水仙花数-C语言和python实现

C语言代码 #include<stdio.h>int main(){int i100,j0;int count0;int num0;for (; i < 1000; i){count i;for(;j<3;j){num num (count%10)*(count%10)*(count%10);count (int)(count/10);}if (numi){/* code */printf("%d是一个水仙花数\n",num);nu…

【解决问题】---- 解决 avue-crud 表格勾选数据翻页后界面保持选中

1. 错误预览 第一页选择【7、8、9、10】 直接点击第三页未进行选择 直接点击第四页未进行选择 2. 问题总结 通过测试可以看到&#xff0c;页面的选择项会影响到其他页面的选择&#xff1b;点击保存&#xff0c;返回的数据却是真真选择的数据&#xff1b;数据在选择渲染…

【论文阅读】多模态NeRF:Cross-Spectral Neural Radiance Fields

https://cvlab-unibo.github.io/xnerf-web intro 从不同的light spectrum sensitivity获取信息&#xff0c;同时需要obtain a unified Cross-Spectral scene representation – allowing for querying, for any single point, any of the information sensed across spectra。…

@Autowired注入其他模块的bean失败

一、在Spring Boot中&#xff0c;如果要自动扫描和创建bean&#xff0c;需要在应用的启动类上使用ComponentScan注解来指定要扫描的包。确保你已经将GlobalConfig类所在的包和父包都添加到了ComponentScan注解的value属性中。 例如&#xff0c;如果GlobalConfig类所在的包为com…

CAN轴【禾川】

禾川CAN轴有问题。 厂家说是只能使用禾川的伺服X2EN&#xff0c;和X3EN 添加CAN主站&#xff1a; 网络&#xff1a; 0 波特率&#xff1a; 1000K 添加CAN总线&#xff1a; 主站&#xff1a; 2 同步帧&#xff1a; 80h 设置刷新时间 时间帧&#xff1a;100h 添加伺服&…

Vue el-table序号与复选框hover切换

效果图下&#xff1a; <template><div class"container"><el-tableref"multipleTable"id"multipleTable":data"person.tableData"cell-mouse-enter"cellEnter"cell-mouse-leave"cellLeave"selecti…

VSCode remote-ssh 连接远端服务器失败

系统 Mac os Intel处理器 描述 该问题在上午时还没有&#xff0c;下午突然毫无征兆的发生&#xff0c;当时没有更新vscode&#xff0c;没有更新插件。 分析 网上对于该问题的答案多是说磁盘空间不够vscode不能下载相应插件&#xff0c;我所遇到的并不是这种情况。报的错误多是…

Leetcode-LCR 021 删除链表的倒数第 N 个结点

快慢指针&#xff0c;快指针先移动n-1个节点后&#xff0c;慢指针从虚拟头结点出发&#xff08;相当于快慢指针相隔n个节点&#xff09;&#xff0c;快慢指针一起向链表尾依次移动一个结点&#xff0c;当快指针移动到表位时&#xff0c;慢指针正好移到被删除元素的前一个结点&a…

基于$indexStats 构建mongodb 所有集合的索引监控程序

使用mongodb时&#xff0c;为集合中经常查询的字段添加索引&#xff0c;能够提高查询效率&#xff0c;减少扫描数据次数&#xff0c;限制扫描数据量&#xff0c;减少排序时间和内存与CPU的消耗。但索引的更新会为数据的添加和更新带来额外的消耗。因此&#xff0c;需要定期检查…

Python数据容器(字符串)

字符串 1.字符串 字符串也是数据容器的一种&#xff0c;字符串是字符的容器&#xff0c;一个字符串可以存放任意数量的字符。 2.字符串的下标索引 从前向后&#xff0c;下标从0开始从后向前&#xff0c;下标从-1开始 # 通过下标索引获取特定位置的字符 name python print(na…

关于使用template时的错误注意

今天在写c程序的时候&#xff0c;在使用template时遇到了一个关于template的错误用法。代码如下#include <vector> #include <algorithm> #include <forward_list> #include <string> #include <list> using namespace std; template <typena…

TinyEngine 开源低代码引擎首次直播答疑QA合集

前言 10月27日晚8点&#xff0c;OpenTiny 社区开启了 TinyEngine 开源低代码引擎首次答疑直播&#xff0c;本次直播我们通过收集开发者诉求&#xff0c;精心策划和组织了内容&#xff0c;希望提供给大家最明确和清晰的答疑方式。这是 TinyEngine 低代码引擎直播计划的开端&…

React处理用户交互事件,如点击、输入框变化等,并使用事件处理函数来响应这些事件

在 React 中处理用户交互事件&#xff0c;如点击、输入框变化等&#xff0c;需要使用事件处理函数来响应这些事件。以下是一些学习和使用事件处理函数的基本步骤&#xff1a; 绑定事件处理函数&#xff1a; 首先&#xff0c;在你的组件中为要处理的元素&#xff08;如按钮、输…

探索OpenCV中直方图的神奇之处:应用与实现

文章目录 导言&#xff1a;直方图概述&#xff1a;函数原型参数说明&#xff1a;代码示例 应用场景&#xff1a;结语&#xff1a; 导言&#xff1a; 直方图是数字图像处理中一个强大而重要的工具&#xff0c;它通过可视化数据的分布情况&#xff0c;帮助我们更好地理解图像的特…

Flutter 第三方 flutter_screenutil(屏幕适配)

一直觉得自己写的不是技术&#xff0c;而是情怀&#xff0c;一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的&#xff0c;希望我的这条路能让你们少走弯路&#xff0c;希望我能帮你们抹去知识的蒙尘&#xff0c;希望我能帮你们理清知识的脉络&#xff0…

【Royalty in Wind 2.0.0】个人体测计算、资料分享小程序

前言 Royalty in Wind 是我个人制作的一个工具类小程序。主要涵盖体测计算器、个人学习资料分享等功能。这个小程序在2022年第一次发布&#xff0c;不过后来因为一些原因暂时搁置。现在准备作为我个人的小程序重新投入使用XD PS&#xff1a;小程序开发部分我是在21年跟随郄培…

vue3异步组件

父组件中&#xff0c;子组件的加载一般是按照先后顺序加载的&#xff0c;子组件加载后才会加载父组件。 一个页面的子组件很多&#xff0c;由于会先加载子组件&#xff0c;那么父组件可能会出现比较长的白屏等待时间 大型项目&#xff0c;可能需要拆分应用为更小的块&#xf…

FL Studio21.2宿主软件中文免费版下载

纵观当下宿主软件市场&#xff0c;正值百家争鸣、百花齐放之际像Mac系统的Logic Pro X、传统宿主软件代表Cubase、录音师必备Pro Tools、后起之秀Studio One等&#xff0c;都在各自的领域具有极高的好评度。而在众多宿主软件中&#xff0c;有这么一款历久弥新且长盛不衰的独特宿…