自定义view(13)自定义属性
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)自定义属性的更多相关文章
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
Android 高手进阶(21) 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明地址:http://blog.csdn.net/xiaanming/article/detail ...
- 手机安全卫士——在设置中心 自定义view和自定义属性
自定义组合控件 1. 自定义一个View, 继承ViewGroup,比如RelativeLayout,此文中是SettingItemView 2. 编写组合控件的布局文件,在自定义的View中加载 ...
- Android 自定义View修炼-自定义View-带百分比进度的圆形进度条(采用自定义属性)
很多的时候,系统自带的View满足不了我们功能的需求,那么我们就需要自己来自定义一个能满足我们需求的View,自定义View我们需要先继承View,添加类的构造方法,重写父类View的一些方法,例如o ...
- Android初级教程初谈自定义view自定义属性
有些时候,自己要在布局文件中重复书写大量的代码来定义一个布局.这是最基本的使用,当然要掌握:但是有些场景都去对应的布局里面写对应的属性,就显得很无力.会发现,系统自带的控件无法满足我们的要求,这个时候 ...
- 【Android - 自定义View】之自定义View浅析
1.概述 Android自定义View / ViewGroup的步骤大致如下: 1) 自定义属性: 2) 选择和设置构造方法: 3) 重写onMeasure()方法: 4) 重写onDraw()方法: ...
- Android 自定义view (一)——attr 理解
前言: 自定义view是android自定义控件的核心之一,那么在学习自定义view之前,我们先来了解下自定义view的自定义属性的attr的用法吧 Android attr 是什么 (1)attr ...
- 自定义View的基本流程
1.明确需求,确定你想实现的效果2.确定是使用组合控件的形式还是全新自定义的形式,组合控件即使用多个系统控件来合成一个新控件,你比如titilebar,这种形式相对简单,参考:http://blog. ...
- Android查缺补漏(View篇)--自定义 View 的基本流程
View是Android很重要的一部分,常用的View有Button.TextView.EditView.ListView.GridView.各种layout等等,开发者通过对这些View的各种组合以 ...
- Android自定义View学习笔记(一)
绘制基础 参考:HenCoder Android 开发进阶: 自定义 View 1-1 绘制基础 Paint详解 参考:HenCoder Android 开发进阶: 自定义 View 1-2 Pain ...
随机推荐
- org.hibernate.id.IdentifierGenerationException错误解决方法
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before ...
- C/C++用状态转移表联合函数指针数组实现状态机FSM
状态机在project中使用很的频繁,有例如以下常见的三种实现方法: 1. switch-case 实现.适合简单的状态机. 2. 二维状态表state-event实现.逻辑清晰.可是矩阵通常比較稀疏 ...
- SpringMVC实战(注解)
1.前言 前面几篇介绍了SpringMVC中的控制器以及视图之间的映射方式,这篇来解说一下SpringMVC中的注解,通过注解能够非常方便的訪问到控制器中的某个方法. 2.配置文件配置 2.1 注解驱 ...
- querying rpm database
Call dbMatch on a transaction set to create a match iterator. As with the C API, a match iterator al ...
- Shell hook
[目的]实现 ll -as +hook+ clear Shell脚本及钩子 - CSDN博客 https://blog.csdn.net/shengzhu1/article/details ...
- phpexcel导出后乱码或者是打不开文件必须修复的问题
百度了一下找到了解决办法,只要在header前面加上ob_end_clean();这句代码,清除缓冲区,这样就可以了,完美的解决了我的问题
- SD/MMC异同
该文章转自:http://www.imhan.com/archives/12/ 经常看到SD/MMC这样的写法,在这里稍微总结一下SD卡和MMC卡的异同点吧. 首先,两者在外型的规格上是几乎一致的.而 ...
- 关于animate的一些属性
animate() 方法执行 CSS 属性集的自定义动画.该方法通过CSS样式将元素从一个状态改变为另一个状态.CSS属性值是逐渐改变的,这样就可以创建动画效果.只有数字值可创建动画(比如 " ...
- FZU1977 Pandora adventure —— 插头DP
题目链接:https://vjudge.net/problem/FZU-1977 Problem 1977 Pandora adventure Accept: 597 Submit: 2199 ...
- HDU - 1875 畅通工程再续(最小生成树)
d.c个小岛,通过建立桥,使其全部可达.求所有的桥的最小长度和. s.最小生成树,数据改成double就行了 c.Prim算法:cost[a][b]和cost[b][a]都得赋值. /* Prim算法 ...