自定义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 ...
随机推荐
- HTML canvas
什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canvas 拥有多种绘制路径.矩形.圆形.字符以 ...
- kbmMemTable关于内存表的使用,以及各种三层框架的评价
关于内存表的使用(kbmMemTable) 关于内存表的使用说明一. Delphi使用内存表1.1 Delphi创建内存表步骤:1. 创建一个Ttable实例.2. 设置一个DataBaseName为 ...
- Linux时间子系统之三:时间的维护者:timekeeper【转】
本文转载自:http://blog.csdn.net/droidphone/article/details/7989566 本系列文章的前两节讨论了用于计时的时钟源:clocksource,以及内核内 ...
- HDU3533 Escape —— BFS / A*算法 + 预处理
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 Escape Time Limit: 20000/10000 MS (Java/Others) ...
- 在织梦dedecms中实现“文章标题-栏目名称-网站名”导航
本文介绍了在dedecms中,实现文章标题-栏目名称-网站名 导航的方法,有需要的朋友参考下. 在dedecms中实现“文章标题-栏目名称-网站名”导航的方法. 第一种: 在/include/in ...
- Python安装pip3常见问题
安装pip3 1.安装 zlib组件: 安装完成后,执行命令 python3 -m pip install redis,报错: RuntimeError: Compression requires t ...
- 安装Sublime Text 3插件的方法(转自Rising的博文)
安装Sublime Text 3插件的方法: 朋友们,小站活着不容易,全靠广告费养着了,如果本文对你有帮助.麻烦动下手点下页面的广告吧,谢谢! 直接安装 安装Sublime text 2插件很方便,可 ...
- MySql必知必会内容导图
<MySQL必知必会>从介绍简单的数据检索开始,逐步深入一些复杂的内容,包括联结的使用.子查询.正则表达式和基于全文本的搜索.存储过程.游标.触发器.表约束,等等.通过重点突出的章节,条理 ...
- bzoj 3073 Journeys —— 线段树优化连边
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3073 建两棵线段树,一棵从下往上连边,一棵从上往下连边,叶子节点之间也有连边: 区间向区间连 ...
- 【旧文章搬运】CsrssWalker学习笔记
原文发表于百度空间及看雪论坛,2009-05-13 看雪论坛地址:https://bbs.pediy.com/thread-89708.htm============================= ...