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. android中Logcat的深层理解

    Android的开发也能够归类为嵌入式设备的开发.即便不是嵌入式开发,依旧要注意对内存和处理的使用.养成一个好的习惯对自己的帮助是非常大的. 在Log的源代码中能够看到这种凝视: The order ...

  2. 注册Dev的帮助文件

    Download the CHM files from… Code: https://www.devexpress.com/Support/Documentation/download.xml?pla ...

  3. ThinkPhp学习01

    原文:ThinkPhp学习01 一.ThinkPHP的介绍           MVC  M - Model 模型                工作:负责数据的操作  V - View  视图(模板 ...

  4. [Android学习笔记]Android中多线程开发的一些概念

    线程安全: 在多线程的情况下,不会因为线程之间的操作而导致数据错误. 线程同步: 同一个资源,可能在同一时间被多个线程操作,这样会导致数据错误.这是一个现象,也是一个问题,而研究如何解决此类问题的相关 ...

  5. SilkTest Q&A 6

    Q51.GMO在线的问题? 该问题是一个特例,不具有代表性,故不翻译了. Q52.如何为一个testplan的属性定义值? A52:你必须在使用前为一个testplan定义值: 1.确保你的test ...

  6. 在浏览器中通过bartender,调用条码打印机的active控件代码的实现

    系统中须要在浏览器,直接调用条码打印机,打印出产品条码. 现实中的条码打印机,品种繁多,非常难在一个程序中实现, 于是我们用已经支持全部条码打印机的bartender软件 调用它的api ,来实如今浏 ...

  7. [Windows Phone学习笔记]UserControl的使用

    UserControl的使用 开发过程中,多个UI控件需要协同工作,相互交互之后,才可完成一个完整的业务需求,此时可把这些控件封装成为一个整体,相互之间的交互逻辑封装其中,外部调用可无需关心内部逻辑, ...

  8. Virtualbox mouse move in and out and file share with windows

    How to use Virstalbox to share files with Linux and Windows, and to move the mouse in and out Virtua ...

  9. Codeforce 143B - Help Kingdom of Far Far Away 2

    B. Help Kingdom of Far Far Away 2 time limit per test 2 seconds memory limit per test 256 megabytes ...

  10. 通过memcached来实现对tomcat集群中Session的共享策略

    近期在做一套集群的实现,实现的方案是在Linux下完成对Apache + Tomcat 负载均衡的功能. 上述功能已经实现,有需要了解的朋友可以看我另外一篇博文. Linux下Apache与Tomca ...