做Android布局是件很享受的事,这得益于他良好的xml方式。使用xml可以快速有效的为软件定义界面。可是有时候我们总感觉官方定义的一些基本组 件不够用,自定义组件就不可避免了。那么如何才能做到像官方提供的那些组件一样用xml来定义他的属性呢?现在我们就来讨论一下他的用法。
1、添加文件attrs.xml,位于res\values目录下:

<?xml version="1.0" encoding="utf-8"?>
<resources> <declare-styleable name="myView"> <attr name="textColor" format="color"/> <attr name="textSize" format="dimension"/> </declare-styleable>
</resources>

2、创建自定义控件:

package com.szy.custom; import com.szy.custom.R; import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View; public class MyView extends View { private Paint myPaint; private static final String myString = "Hello CustomView!"; public MyView(Context context) { super(context); // TODO Auto-generated constructor stub
     } public MyView(Context context, AttributeSet attr) { super(context, attr); myPaint = new Paint(); TypedArray a = context.obtainStyledAttributes(attr, R.styleable.myView);//TypedArray是一个数组容器 float textSize = a.getDimension(R.styleable.myView_textSize, 30);//防止在XML文件里没有定义,就加上了默认值30 int textColor = a.getColor(R.styleable.myView_textColor, 0xFFFFFFFF);//同上,这里的属性是:名字_属性名
         myPaint.setTextSize(textSize); myPaint.setColor(textColor); a.recycle();//我的理解是:返回以前取回的属性,供以后使用。以前取回的可能就是textSize和textColor初始化的那段
     } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub
        super.onDraw(canvas); //myPaint = new Paint();
         myPaint.setColor(Color.RED); myPaint.setStyle(Style.FILL); canvas.drawRect(new Rect(10,10,100,100), myPaint); myPaint.setColor(Color.WHITE); canvas.drawText(myString, 10, 100, myPaint); }
}

3、在Activity布局文件中使用自定义控件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>" xmlns:test="<a href="http://schemas.android.com/apk/res/com.szy.custom">http://schemas.android.com/apk/res/com.szy.custom</a>" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<TextView   android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="@string/hello" />
<com.adnroid.test.MyView android:layout_width="fill_parent" android:layout_height="fill_parent" test:textSize="10px" test:textColor="#fff" />
</LinearLayout>

附:Android中自定义属性的格式详解
1. reference:参考某一资源ID。(1)属性定义:<declare-styleable name = "名称"><attr name = "background" format = "reference" /></declare-styleable>(2)属性使用:<ImageViewandroid:layout_width = "42dip"android:layout_height = "42dip"android:background = "@drawable/图片ID"/>
2. color:颜色值。(1)属性定义:<declare-styleable name = "名称"><attr name = "textColor" format = "color" /></declare-styleable>(2)属性使用:<TextViewandroid:layout_width = "42dip"android:layout_height = "42dip"android:textColor = "#00FF00"/>
3. boolean:布尔值。(1)属性定义:<declare-styleable name = "名称"><attr name = "focusable" format = "boolean" /></declare-styleable>(2)属性使用:<Buttonandroid:layout_width = "42dip"android:layout_height = "42dip"android:focusable = "true"/>
4. dimension:尺寸值。(1)属性定义:<declare-styleable name = "名称"><attr name = "layout_width" format = "dimension" /></declare-styleable>(2)属性使用:<Buttonandroid:layout_width = "42dip"android:layout_height = "42dip"/>
5. float:浮点值。(1)属性定义:<declare-styleable name = "AlphaAnimation"><attr name = "fromAlpha" format = "float" /><attr name = "toAlpha" format = "float" /></declare-styleable>(2)属性使用:<alphaandroid:fromAlpha = "1.0"android:toAlpha = "0.7"/>
6. integer:整型值。(1)属性定义:<declare-styleable name = "AnimatedRotateDrawable"><attr name = "visible" /><attr name = "frameDuration" format="integer" /><attr name = "framesCount" format="integer" /><attr name = "pivotX" /><attr name = "pivotY" /><attr name = "drawable" /></declare-styleable>(2)属性使用:<animated-rotatexmlns:android = "http://schemas.android.com/apk/res/android" android:drawable = "@drawable/图片ID" android:pivotX = "50%" android:pivotY = "50%" android:framesCount = "12" android:frameDuration = "100"/>
7. string:字符串。(1)属性定义:<declare-styleable name = "MapView"><attr name = "apiKey" format = "string" /></declare-styleable>(2)属性使用:<com.google.android.maps.MapViewandroid:layout_width = "fill_parent"android:layout_height = "fill_parent"android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"/>
8. fraction:百分数。(1)属性定义:<declare-styleable name="RotateDrawable"><attr name = "visible" /><attr name = "fromDegrees" format = "float" /><attr name = "toDegrees" format = "float" /><attr name = "pivotX" format = "fraction" /><attr name = "pivotY" format = "fraction" /><attr name = "drawable" /></declare-styleable>(2)属性使用:<rotate  xmlns:android = "http://schemas.android.com/apk/res/android"android:interpolator = "@anim/动画ID"android:fromDegrees = "0"android:toDegrees = "360"android:pivotX = "200%"android:pivotY = "300%"android:duration = "5000"android:repeatMode = "restart"android:repeatCount = "infinite"/>
9. enum:枚举值。(1)属性定义:<declare-styleable name="名称"><attr name="orientation"><enum name="horizontal" value="0" /><enum name="vertical" value="1" /></attr>           </declare-styleable>(2)属性使用:<LinearLayoutxmlns:android = "http://schemas.android.com/apk/res/android"android:orientation = "vertical"android:layout_width = "fill_parent"android:layout_height = "fill_parent"></LinearLayout>
10. flag:位或运算。(1)属性定义:<declare-styleable name="名称"><attr name="windowSoftInputMode"><flag name = "stateUnspecified" value = "0" /><flag name = "stateUnchanged" value = "1" /><flag name = "stateHidden" value = "2" /><flag name = "stateAlwaysHidden" value = "3" /><flag name = "stateVisible" value = "4" /><flag name = "stateAlwaysVisible" value = "5" /><flag name = "adjustUnspecified" value = "0x00" /><flag name = "adjustResize" value = "0x10" /><flag name = "adjustPan" value = "0x20" /><flag name = "adjustNothing" value = "0x30" /></attr>        </declare-styleable>(2)属性使用:<activityandroid:name = ".StyleAndThemeActivity"android:label = "@string/app_name"android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden"><intent-filter><action android:name = "android.intent.action.MAIN" /><category android:name = "android.intent.category.LAUNCHER" /></intent-filter></activity>注意:属性定义时可以指定多种类型值。(1)属性定义:<declare-styleable name = "名称"><attr name = "background" format = "reference|color" /></declare-styleable>(2)属性使用:<ImageViewandroid:layout_width = "42dip"android:layout_height = "42dip"android:background = "@drawable/图片ID|#00FF00"/>

转载于:https://www.cnblogs.com/zhujiabin/p/4538032.html

Android 自定义属性(attrs.xml,TypedArray)相关推荐

  1. android 参数 attrs.xml,使用attrs.xml自定义属性

    控件有很多属性,如android:id.android:layout_width.android:layout_height等,但是这些属性都是系统自带的属性.使用attrs.xml文件,可以自己定义 ...

  2. android开发:Android 中自定义属性(attr.xml,TypedArray)的使用

    今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三)查看第三课,这样跟容易方便你的理解! 在xml 文件里定义控件的属性,我们已经习惯了android:att ...

  3. Android 自定义View二(深入了解自定义属性attrs.xml)

    1.为什么要自定义属性 要使用属性,首先这个属性应该存在,所以如果我们要使用自己的属性,必须要先把他定义出来才能使用.但我们平时在写布局文件的时候好像没有自己定义属性,但我们照样可以用很多属性,这是为 ...

  4. Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!

    今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三) 查看第三课,这样跟容易方便你的理解! 在xml 文件里定义控件的属性,我们已经习惯了android:at ...

  5. Android中attrs.xml文件的使用详解

    $*********************************************************************************************$ 博主推荐 ...

  6. [Android自定义控件]自定义属性attrs.xml中format

    前言 在我们自定义控件的时候,需要自己定义布局xml对象属性,就需要styles.xml自定义,然后再自定义java文件中获取信息,记录方便自己使用 使用 xml首先需要自定义命名空间: xmlns: ...

  7. Android中自定义属性(attrs.xml,TypedArray的使用)

    做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了.那么如何才能做到像官方 ...

  8. Android 自定义属性时TypedArray的使用

    对于自定义属性,遵循以下几步,就可以实现: 自定义一个CustomView(extends View )类 编写res/values/attrs.xml,在其中编写styleable和item等标签元 ...

  9. android自定义属性的使用

     最近在学习一个开源的项目,看到人家定义的资源文件有如下标签: 而在该项目中,利用以上路径追溯下去,会追溯到这么一个类文件,所以就迷糊了,定义布局文件跟类有毛关系<比较二>查了下 原来 ...

最新文章

  1. debian10 dhcp简单配置
  2. wps android qq 群,手机WPS怎样发送文档给QQ?WPS怎样发送文档给QQ教程
  3. 【特征工程】(未完成)特征选择
  4. 云原生生态周报 Vol. 19 | Helm 推荐用户转向 V3
  5. oracle使用唯一结果集,oracle分页查询结果集重复问题解决方法
  6. 操作系统-信号量的使用
  7. centos7 firewall指定IP与端口访问(常用)
  8. 2021 高考 成绩查询,精准预测!2021全国大学录取分数线表查询
  9. oracle数据库存储ip地址,oracle – 以十进制形式存储的IP地址 – PL / SQL以虚线四边形显示...
  10. 好棒,测试妹子都能看懂的Jenkins Docker安装教程
  11. 浅谈如何用We7站群平台打造垂直性政务网站
  12. nodejs基础 -- 全局对象
  13. Linux多线程编程之pthread
  14. 【干货】大学本科生零基础如何开始做发明类竞赛项目
  15. 嵌入式 Linux 启动时间优化
  16. 如何干掉腾讯网迷你版
  17. Android PopupWindow 的方法 弹出窗口方法
  18. mp3音频格式在线转换器 在线转换MP3格式
  19. c语言解三色旗问题加注释,C语言经典算法——三色旗问题
  20. mysql面试题总结

热门文章

  1. php getdbused,PHP之购物车
  2. Java 基础知识总结(下)-王者笔记《收藏版》
  3. HTML+CSS+JS实现 ❤️酷炫彩虹旋转隧道特效❤️
  4. 取值方法_「EV3进阶课」制作小游戏:数据取值体系要统一(三)
  5. 看门狗性能软件测试,《看门狗:军团》PC版性能测试 不建议光追,优化极差
  6. qt5python gui cookbook_Python GUI Programming Cookbook学习笔记
  7. SQLServer查找已知数相邻前后数
  8. linux 内核 82540网卡,Linux网卡as4.2 编译安装及配置准备
  9. oracle数据如何获取游标中动态字段_原来Python自带了数据库,用起来真方便!
  10. 没有bug队——加贝——Python 练习实例 23,24