Android_declare-styleable_自己定义控件的属性
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_自己定义控件的属性的更多相关文章
- android--------自定义控件 之 属性篇
上篇介绍了自定义控件的一个简单案例,本篇文章主要介绍如何给自定义控件自定义一些属性. Android 中使用自定义属性的一般步骤: 定义declare-styleable,添加attr 使用Typed ...
- Android -- shape 定义控件的属性
<shape> <!-- 实心 --> <solid android:color="#ff9d77"/> <!-- 渐变 --> & ...
- android 自己定义控件属性(TypedArray以及attrs解释)
近期在捣鼓android 自己定义控件属性,学到了TypedArray以及attrs.在这当中看了一篇大神博客Android 深入理解Android中的自己定义属性.我就更加深入学习力一番.我就沿着这 ...
- Android自定义控件1--自定义控件介绍
Android控件基本介绍 Android本身提供了很多控件比如我们常用的有文本控件TextView和EditText:按钮控件Button和ImageButton状态开关按钮ToggleButton ...
- android控件的属性
android控件的属性 本节描述android空间的位置,内容等相关属性及属性的含义 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 ( ...
- Android自己定义控件之应用程序首页轮播图
如今基本上大多数的Android应用程序的首页都有轮播图.就是像下图这种(此图为转载的一篇博文中的图.拿来直接用了): 像这种组件我相信大多数的应用程序都会使用到,本文就是自己定义一个这种组件,能够动 ...
- 在C#中使用属性控件添加属性窗口
转自原文 在C#中使用属性控件添加属性窗口 第一步,创建在应用程序中将要展现的字段属性为public公有属性.其中,所有的属性必须有get和set的方法(如果不设置get方法,则要显示的属性不会显示在 ...
- Android中如何利用attrs和styles定义控件
一直有个问题就是,Android中是如何通过布局文件,就能实现控件效果的不同呢?比如在布局文件中,我设置了一个TextView,给它设置了 textColor,它就能够改变这个TextView的文本的 ...
- Android自己定义控件(状态提示图表)
[工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重分享成果] 1 背景 前面分析那么多系统源代码了.也该暂停下来歇息一下,趁昨晚闲着看见一个有意思的需求就操 ...
随机推荐
- 怎么提高ArcGIS for Desktop10.x的性能
Esri新公布了一篇提高ArcGIS for Desktop10.x的性能的文章.大家能够关注一下 http://support.esri.com/en/knowledgebase/techartic ...
- LeetCode——Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- Git权威指南学习笔记(二)Git暂存区
例如以下图所看到的: 左側为工作区,是我们的工作文件夹. 右側为版本号库,当中: index标记的是暂存区(stage),所处文件夹为.git/index,记录了文件的状态和变更信息. master标 ...
- NSHashTable 和 NSMapTable学习
今天,在实现play gif时间功能,我看见两个陌生班,只需看看这个纪录: NSSet和NSDictionary是两个经常使用的类,可是他们默认假定了当中对象的内存行为.对于NSSet.object是 ...
- Swift - 带结果列表的搜索条(UISearchDisplayController)的用法
(注:自iOS8起,苹果便废弃UISearchDisplayController的使用,改为使用UISearchController来实现类似功能,可参考我的另一篇文章“Swift - 使用UISea ...
- linux df和du统计的空间不一致
假设你发现是 /tmp/ 目录不对劲,那么就这样就可以查看: lsof | grep /tmp 输出的结果中,注意某些含有“(deleted)”字样的记录,它们中的一部分就是罪魁祸首,将它们kill ...
- WinRarHelper帮助类
WinRarHelper帮助类 关于本文档的说明 本文档使用WinRAR方式来进行简单的压缩和解压动作,纯干货,实际项目这种压缩方式用的少一点,一般我会使用第三方的压缩dll来实现,就如同我上一个压缩 ...
- Android短信监听(二)——利用ContentObserver实现短信监听
MainActivity例如以下: package cc.testsmslistener; import cc.testsmslistener.SMSContentObserver.MessageLi ...
- JSF之经常使用注解
@ManagedBean 以托管 bean 的形式注冊一个类实例.然后将其放入到使用当中一个 @...Scoped 凝视指定的范围内.假设没有指定不论什么范围.JSF 将把此 bean 放入请求范围. ...
- 使用CocoaPods出现 The `master` repo requires CocoaPods 0.32.1 - 问题解决
近期在使用CocoaPods为project配置第三方类库时出现了例如以下问题: [!] The `master` repo requires CocoaPods 0.32.1 - 明显是由于Coco ...