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. debian iptables持久化

    1 保存iptables iptables-save > /etc/iptables.rules   2 创建启动文件 touch /etc/network/if-pre-up.d/iptabl ...

  2. 简易SQL语句

    /*创建 模式 为用户 User1*/ CREATE SCHEMA test authorization User1; CREATE SCHEMA test USER User1; CREATE TA ...

  3. rails elasticsearch searchkick用法

    1.安装elasticsearch 之前要先安装java8: 参考https://www.elastic.co/guide/en/elasticsearch/reference/current/zip ...

  4. 一步一步学Silverlight 2系列(32):图形图像综合实例—“功夫之王”剧照播放

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  5. UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)

    d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍 ...

  6. Apriori算法实例

    Apriori算法与实例 R. Agrawal 和 R. Srikant于1994年在文献[2]中提出了Apriori算法,该算法的描述如下: 下面是一个具体的例子,最开始数据库里有4条交易,{A.C ...

  7. Intel® Media SDK(一)

    A cross-platform API for developing media applications on Windows* Fast video playback, encode, proc ...

  8. [Java] public, private, protected

      同包不同类的成员 不同包中子类的成员 不同包非子类的成员 public √ √ √ protected √ √ × 默认 √ × × private × × ×

  9. PHP自动发送邮件

    目录 1. PHPMailer 2. 集成ThinkPHP 2.1 类库重命名 2.2 配置SMTP服务器 2.3 使用 1. PHPMailer 在自己项目引入核心类库文件 require_once ...

  10. 洛谷P1290 欧几里德的游戏

    题目:https://www.luogu.org/problemnew/show/P1290 只要出现n>=2*m,就可以每次把较大的数控制在较小的数的一倍与二倍之间,则控制了对方的走法: 每次 ...