1.简单实例

(1).在res/values文件下定义一个attrs.xml文件

<?

xml version="1.0" encoding="utf-8"?

>
<resources> <declare-styleable name="my">
<attr name="content" format="string" />
<attr name="backcolor" format="reference|color" />
</declare-styleable> </resources>

(2).布局文件使用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:my="http://schemas.android.com/apk/res/com.example.xmpptest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <com.example.xmpptest.MyTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
my:content="mycolor" >
</com.example.xmpptest.MyTextView> </LinearLayout>

(3).自己定义控件设置

public class MyTextView extends TextView {

	public MyTextView(Context context) {
this(context, null);
} public MyTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.my);
String str = a.getString(R.styleable.my_content);
setText(str + "@@");
a.recycle();
} }

2.其他备注

(1).定义的每一个attr中format应该为指定的类型,string/integer/dimension/reference(引用R文件)/color/enum/boolean。多个format能够使用|隔开,其他都一样。实例enum

<declare-styleable name="my">
<attr name="content" format="string" />
<attr name="backcolor" format="reference|color" />
<attr name="size">
<enum name="fill_parent" value="-1" />
<enum name="wrap_content" value="-2" />
</attr>
</declare-styleable>

(2).布局文件里的XML namespace规则。xmlns:toolbar=http://schemas.android.com/apk/res/[packagename]

Android_declare-styleable_自己定义控件的属性的更多相关文章

  1. android--------自定义控件 之 属性篇

    上篇介绍了自定义控件的一个简单案例,本篇文章主要介绍如何给自定义控件自定义一些属性. Android 中使用自定义属性的一般步骤: 定义declare-styleable,添加attr 使用Typed ...

  2. Android -- shape 定义控件的属性

    <shape> <!-- 实心 --> <solid android:color="#ff9d77"/> <!-- 渐变 --> & ...

  3. android 自己定义控件属性(TypedArray以及attrs解释)

    近期在捣鼓android 自己定义控件属性,学到了TypedArray以及attrs.在这当中看了一篇大神博客Android 深入理解Android中的自己定义属性.我就更加深入学习力一番.我就沿着这 ...

  4. Android自定义控件1--自定义控件介绍

    Android控件基本介绍 Android本身提供了很多控件比如我们常用的有文本控件TextView和EditText:按钮控件Button和ImageButton状态开关按钮ToggleButton ...

  5. android控件的属性

    android控件的属性 本节描述android空间的位置,内容等相关属性及属性的含义 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 ( ...

  6. Android自己定义控件之应用程序首页轮播图

    如今基本上大多数的Android应用程序的首页都有轮播图.就是像下图这种(此图为转载的一篇博文中的图.拿来直接用了): 像这种组件我相信大多数的应用程序都会使用到,本文就是自己定义一个这种组件,能够动 ...

  7. 在C#中使用属性控件添加属性窗口

    转自原文 在C#中使用属性控件添加属性窗口 第一步,创建在应用程序中将要展现的字段属性为public公有属性.其中,所有的属性必须有get和set的方法(如果不设置get方法,则要显示的属性不会显示在 ...

  8. Android中如何利用attrs和styles定义控件

    一直有个问题就是,Android中是如何通过布局文件,就能实现控件效果的不同呢?比如在布局文件中,我设置了一个TextView,给它设置了 textColor,它就能够改变这个TextView的文本的 ...

  9. Android自己定义控件(状态提示图表)

    [工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重分享成果] 1 背景 前面分析那么多系统源代码了.也该暂停下来歇息一下,趁昨晚闲着看见一个有意思的需求就操 ...

随机推荐

  1. python 时间戳 datetime string 转换

    import datetime import time **datetime转时间戳** In [1]: now = datetime.datetime.now() In [2]: time.mkti ...

  2. 获取Exe文件版本信息的函数(使用GetFileVersionInfo得到TFileVersionInfo结构体,包含12项内容)

    Type   TFileVersionInfo   =   Record         FixedInfo:TVSFixedFileInfo;         {版本信息}         Comp ...

  3. js封装的类似java StringBuilder类

    使用js的时候,经常会使用字符串拼接,但是在IE6和IE7没有对+作优化,所以性能会很低,鉴于此,我封装了StringBuilder类,用于拼接字符串,直接把代码贴出来如下: function Str ...

  4. Div 设置最小宽度。 对喜欢设置百分比的同学很用有哦,

    第一种情况:  body { width:100%; higth:100%; } 当浏览器缩小的时候,若是body里有浮动元素.或者有设置百分比的div   或出现字体挤压情况. 第二种情况: bod ...

  5. (2)入门指南——(7)添加jquery代码(Adding our jQuery code)

    Our custom code will go in the second, currently empty, JavaScript file which we included from the H ...

  6. [置顶] 深圳华为BSS公共部件 (BI 商业智能 Java Javascript)

    深圳华为BSS公共部件 部门招聘 招聘面试地点:大连,西安 工作地点:深圳 时间:2013年9月7日 联系方式:dawuliang@gmail.com 18675538182 有兴趣的同学,可以直接电 ...

  7. 安装Oracle JDK 7.0与8.0 for Mac OS X后Eclipse启动报错的解决之道

    启动 Eclipse 时,直接报错The JVM shared library "/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Cont ...

  8. E. Mike and Foam(容斥原理)

    E. Mike and Foam Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special sh ...

  9. loj1236(数学)

    传送门:Pairs Forming LCM 题意:题意:问符合 lcm(i,j)=n (1<=i<=j<=n,1<=n<=10^14) 的 (i,j) 有多少对. 分析: ...

  10. C# 计算字符串/文件的哈希值(MD5、SHA)

    原文 C# 计算字符串的哈希值(MD5.SHA) 已做修改 一.关于本文 本文中是一个类库,包括下面几个函数: /// 1)计算32位MD5码(大小写):Hash_MD5_32 /// 2)计算16位 ...