1.添加attrs.xml文件

  在android studio下,在res/values 下新建资源文件attrs.xml

2.添加自定义的属性

在attrs.xml中添加属性,如下。其中format是属性的类型,如float,color,boolean,dimension

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="thermograph">
<attr name="minValue" format="float" />
<attr name="maxValue" format="float" />
<attr name="ratio" format="float" />
<attr name="hotValue" format="float" />
<attr name="coldValue" format="float" />
<attr name="thermoValue" format="float" />
<attr name="textSize" format="dimension" />
<attr name="warningColor" format="color" />
<attr name="contourColor" format="color" />
<attr name="textColor" format="color" />
<attr name="borderColor" format="color" />
<attr name="hotColor" format="color" />
<attr name="coldColor" format="color" />
<attr name="valueColor" format="color" />
<attr name="showValue" format="boolean" /> </declare-styleable>
</resources>

3.在布局文件中使用自定义的属性

3.1 在布局文件根结点中添加命名空间。

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:thermograph="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorMainBackground"
>

3.2 使用自定义的属性

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:thermograph="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorMainBackground"
> <com.cnblogs.sjjg.tempview.Thermograph
android:id="@+id/thermograph1"
android:layout_width="10dp"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintBottom_toBottomOf="@+id/thermograph2"
app:layout_constraintEnd_toStartOf="@+id/thermograph2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/thermograph2"
thermograph:borderColor="@color/temp1_color"
thermograph:coldValue="10"
thermograph:hotValue="50"
thermograph:maxValue="70"
thermograph:minValue="-30" /> <com.cnblogs.sjjg.tempview.Thermograph
android:id="@+id/thermograph2"
android:layout_width="100dp"
android:layout_height="250dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
thermograph:coldValue="0"
thermograph:hotValue="50"
thermograph:maxValue="70"
thermograph:minValue="-30" /> <SeekBar
android:id="@+id/seekBar"
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:max="100"
android:progress="50"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/thermograph2" /> </androidx.constraintlayout.widget.ConstraintLayout>

4.在自定义的view中取属性值

     private void init(Context context,AttributeSet attrs){

         paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL); setLayerType(View.LAYER_TYPE_SOFTWARE,paint); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.thermograph);
min = ta.getFloat(R.styleable.thermograph_minValue, -);
max = ta.getFloat(R.styleable.thermograph_maxValue,);
value = ta.getFloat(R.styleable.thermograph_thermoValue,);
hot = ta.getFloat(R.styleable.thermograph_hotValue,);
cold = ta.getFloat(R.styleable.thermograph_coldValue,);
ratio = ta.getFloat(R.styleable.thermograph_ratio,0.25f); txtColor = ta.getColor(R.styleable.thermograph_textColor,Color.WHITE); int c = ContextCompat.getColor(context,R.color.temp_color);
borderColor = ta.getColor(R.styleable.thermograph_borderColor,c); c = ContextCompat.getColor(context,R.color.temp_cold_color);
contourColor = ta.getColor(R.styleable.thermograph_contourColor,c); c = ContextCompat.getColor(context,R.color.temp_hot_color);
hotColor = ta.getColor(R.styleable.thermograph_hotColor,c); c = ContextCompat.getColor(context,R.color.temp_cold_color);
coldColor = ta.getColor(R.styleable.thermograph_coldColor,c); showValue = ta.getBoolean(R.styleable.thermograph_showValue,true); } public Thermograph(Context context) {
super(context);
init(context,null);
} public Thermograph(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context,attrs);
} public Thermograph(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context,attrs);
} @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public Thermograph(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context,attrs);
}

自定义view(13)自定义属性的更多相关文章

  1. Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)

      Android 高手进阶(21)  版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明地址:http://blog.csdn.net/xiaanming/article/detail ...

  2. 手机安全卫士——在设置中心 自定义view和自定义属性

    自定义组合控件 1. 自定义一个View, 继承ViewGroup,比如RelativeLayout,此文中是SettingItemView 2. 编写组合控件的布局文件,在自定义的View中加载   ...

  3. Android 自定义View修炼-自定义View-带百分比进度的圆形进度条(采用自定义属性)

    很多的时候,系统自带的View满足不了我们功能的需求,那么我们就需要自己来自定义一个能满足我们需求的View,自定义View我们需要先继承View,添加类的构造方法,重写父类View的一些方法,例如o ...

  4. Android初级教程初谈自定义view自定义属性

    有些时候,自己要在布局文件中重复书写大量的代码来定义一个布局.这是最基本的使用,当然要掌握:但是有些场景都去对应的布局里面写对应的属性,就显得很无力.会发现,系统自带的控件无法满足我们的要求,这个时候 ...

  5. 【Android - 自定义View】之自定义View浅析

    1.概述 Android自定义View / ViewGroup的步骤大致如下: 1) 自定义属性: 2) 选择和设置构造方法: 3) 重写onMeasure()方法: 4) 重写onDraw()方法: ...

  6. Android 自定义view (一)——attr 理解

    前言: 自定义view是android自定义控件的核心之一,那么在学习自定义view之前,我们先来了解下自定义view的自定义属性的attr的用法吧 Android attr 是什么 (1)attr ...

  7. 自定义View的基本流程

    1.明确需求,确定你想实现的效果2.确定是使用组合控件的形式还是全新自定义的形式,组合控件即使用多个系统控件来合成一个新控件,你比如titilebar,这种形式相对简单,参考:http://blog. ...

  8. Android查缺补漏(View篇)--自定义 View 的基本流程

    View是Android很重要的一部分,常用的View有Button.TextView.EditView.ListView.GridView.各种layout等等,开发者通过对这些View的各种组合以 ...

  9. Android自定义View学习笔记(一)

    绘制基础 参考:HenCoder Android 开发进阶: 自定义 View 1-1 绘制基础 Paint详解 参考:HenCoder Android 开发进阶: 自定义 View 1-2 Pain ...

随机推荐

  1. 使用tencent协议发起临时会话

    调用默认浏览器打开链接tencent://message/?uin=QQ即可发起临时会话参数uin为目标QQ Java示例 import java.awt.Desktop; import java.n ...

  2. linux设备驱动学习笔记(1)

    学习了将近半个月的设备驱动程序的编写,也有一些体会,这里写下来也给学习做一个总结,为后面的学习做更好的准备. 首先,个人感觉驱动程序的设计是很有套路的,最基本的要求就是要掌握这些套路.所谓的套路就是一 ...

  3. 教你开发jQuery插件

    jQuery插件开发模式 软件开发过程中是需要一定的设计模式来指导开发的,有了模式,我们就能更好地组织我们的代码,并且从这些前人总结出来的模式中学到很多好的实践. 根据<jQuery高级编程&g ...

  4. codeforces C. Magic Formulas 解题报告

    题目链接:http://codeforces.com/problemset/problem/424/C 题目意思:给出 n 个数:p1, p2, ..., pn,定义: q1 = p1 ^ (1 mo ...

  5. Python学习笔记_Mysql数据库、Excel

    一.操作mysql数据库 import pymysql # 1.连上数据库:账号,密码,ip,端口号,数据库 # 2.建立游标(去数据库拿东西的工人) # 3.执行sql # 4.获取结果 # 5.关 ...

  6. No tests found with test runner 'JUnit 3'

    报异常:No tests found with test runner 'JUnit 3' 解决方案: 主要因为你当前建的JUnit类是3的版本,将该类备份,重新创建一个类. 1.右键目录New--O ...

  7. android项目 res/ 目录内支持的资源目录详解

    表 1. 项目 res/ 目录内支持的资源目录 目录 资源类型 animator/ 用于定义属性动画的 XML 文件. anim/ 定义渐变动画的 XML 文件.(属性动画也可以保存在此目录中,但是为 ...

  8. [原创]java实现word转pdf

    最近遇到一个项目需要把word 转成pdf,百度了一下网上的方案有很多,比如虚拟打印.给word 装扩展插件等,这些方案都依赖于ms word 程序,在java代码中也得使用诸如jacob或jcom这 ...

  9. python中的编码和解码

    计算机中常见的编码方式有多种,英文一般是ascii编码,其他有unicode,utf-8,gbk,utf-16等编码. 常见编码方式: ASCII编码:ASCII是早期的编码,包含英文字母.数字和 ...

  10. I.MX6 不一定要设置BOOT_MODE进入烧录模式

    /************************************************************************* * I.MX6 不一定要设置BOOT_MODE进入 ...