1.自定义属性格式
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="attr1" format="string" />
<declare-styleable name="MyButton">
<attr name="attr1" />
<attr name="attr2" format="dimension" />
</declare-styleable>
<declare-styleable name="MyButton1">
<attr name="attr1" />
<attr name="attr3" format="dimension" />
</declare-styleable>
</resources>
生成的R.java中的内容
public static final class attr {
public static final int attr1=0x7f01000f;
public static final int attr2=0x7f01001f;
public static final int attr3=0x7f01002f;
}
public static final class styleable {
public static final int[] MyButton = {
0x7f01000f, 0x7f01001f
};
public static final int MyButton_attr1 = 0;
public static final int MyButton_attr2 = 1;
public static final int[] MyButton1 = {
0x7f01000f, 0x7f01002f
};
public static final int MyButton1_attr1 = 0;
public static final int MyButton1_attr3= 1;
}
attr子元素:
定义具体的属性,format表示这个属性的值的类型,类型有以下几种:
1.reference:参考指定Theme中资源ID,这个类型意思就是你传的值可以是引用资源
2.string:字符串,如果你想别人既能直接写值也可以用类似"@string/test"引用资源的方式,可以写
成format="string|reference"
3.Color:颜色
4.boolean:布尔值
5.dimension:尺寸值
6.float:浮点型
7.integer:整型
8.fraction:百分数
9.enum:枚举 ,如果你提供的属性只能让别人选择,不能随便传入,就可以写成这样
<attr name="language">
<enum name="china" value="1"/>
<enum name="English" value="2"/>
</attr>
10.flag:位或运算
declare-styleable子元素:
定义一个styleable对象,每个styleable对象就是一组attr属性的集合 注意:这里的name属性并不是一定
要和自定义类名相同,只是为了好区分对应类的属性而已。多个declare-styleable公用属性要抽出不然会报重复定义。
注意:上面的属性资源文件定义了该属性之后,至于到底是哪个自定义View组件中来使用该属性,该属性到
底能发挥什么作用, 就不归该属性资源文件管了,也就是说这个属性资源文件是个公共的,大家都可以用,
但是为了方便管理,一般都是一个自定义View里的属性写成一个declare-styleable集合.属性资源所定义
的属性到底可以返回什么作用,取决于自定义组件的代码实现。从生成的R文件也能发现这个规律。
2.xml中使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:textbutton="http://schemas.android.com/apk/res/com.easymorse.textbutton"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/background_color">
<com.easymorse.textbutton.MyButton
android:layout_width="fill_parent" android:layout_height="fill_parent"
textbutton:attr1="#ff0000
textbutton:attr1="10dp"/>
</LinearLayout>
这里在根标签中增加了:
xmlns:textbutton=http://schemas.android.com/apk/res/com.easymorse.textbutton
声明了textbutton这个名字空间,textbutton是任意的名称,自己可以随便起名,后面的:
http://schemas.android.com/apk/res/是固定的。再后面接的是应用的包名。
在下面自定义按钮中的:textbutton:attr1,就是使用attr1这个变量了,给变量赋值。
3.代码中获得属性值
public MyButton(final Context context, AttributeSet attrs) {
this(context, attrs, 0);
TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.button);
int attr2=typedArray.getDimensionPixelSize(R.styleable.MyButton_attr2, 15);
typedArray.recycle();
}
这样就取得了attr2的在xml中的值,让后可以程序中应用。
obtainStyledAttributes参数说明
我们在自定义View 时,一般都会用到
TypedArray obtainStyledAttributes(
AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes); 对于它的前面2个参数可能大家都知道, 对于后面两个参数一直不明白怎么用。
于是今天查了下。
context obtainStyledAttributes 最终会调到Resources.Theme.obtainStyledAttributes(AttributeSet set,
int[] attrs, int defStyleAttr, int defStyleRes)
再看一下参数解释:
* @param set The base set of attribute values. May be null.
* @param attrs The desired attributes to be retrieved.
* @param defStyleAttr An attribute in the current theme that contains a
* reference to a style resource that supplies
* defaults values for the TypedArray. Can be
* 0 to not look for defaults.
* @param defStyleRes A resource identifier of a style resource that
* supplies default values for the TypedArray,
* used only if defStyleAttr is 0 or can not be found
* in the theme. Can be 0 to not look for defaults.
defStyleAttr 指向当前theme 某个item 描述的style 该style指定了一些默认值为这个TypedArray
defStyleRes 当defStyleAttr 找不到或者为0, 可以直接指定某个style
不是很理解 ,搜了一下源码:
发现用的地方 不多。如CalendarView
TypedArray attributesArray = context.obtainStyledAttributes(attrs, R.styleable.CalendarView,
R.attr.calendarViewStyle, 0);
CalendarView 在 attrs.xml 中 定义如下:
<declare-styleable name="CalendarView">
<!-- The first day of week according to {@link java.util.Calendar}. -->
<attr name="firstDayOfWeek" format="integer" />
<!-- Whether do show week numbers. -->
<attr name="showWeekNumber" format="boolean" />
<!-- The minimal date shown by this calendar view in mm/dd/yyyy format. -->
<attr name="minDate" />
<!-- The minimal date shown by this calendar view in mm/dd/yyyy format. -->
<attr name="maxDate" />
<!-- The number of weeks to be shown. -->
<attr name="shownWeekCount" format="integer"/>
<!-- The background color for the selected week. -->
<attr name="selectedWeekBackgroundColor" format="color|reference" />
<!-- The color for the dates of the focused month. -->
<attr name="focusedMonthDateColor" format="color|reference" />
<!-- The color for the dates of an unfocused month. -->
<attr name="unfocusedMonthDateColor" format="color|reference" />
<!-- The color for the week numbers. -->
<attr name="weekNumberColor" format="color|reference" />
<!-- The color for the separator line between weeks. -->
<attr name="weekSeparatorLineColor" format="color|reference" />
<!-- Drawable for the vertical bar shown at the beginning and at the end of the selected date. -->
<attr name="selectedDateVerticalBar" format="reference" />
<!-- The text appearance for the week day abbreviation of the calendar header. -->
<attr name="weekDayTextAppearance" format="reference" />
<!-- The text appearance for the calendar dates. -->
<attr name="dateTextAppearance" format="reference" />
</declare-styleable>
默认 defStyleAttr 为 calendarViewStyle
也定义在attrs.xml
<!-- The CalendarView style. -->
<attr name="calendarViewStyle" format="reference" />
再看看在theme.xml 中 它指向:
<!-- CalendarView style--Defalut>
<item name="calendarViewStyle">@style/Widget.CalendarView</item>
。。。。
<!-- CalendarView style--Holo>
<item name="calendarViewStyle">@style/Widget.Holo.CalendarView</item>
。。。
<!-- CalendarView style-HOLOLight->
<item name="calendarViewStyle">@style/Widget.Holo.Light.CalendarView</item>
不同的theme 指向不同的style
如style.xml 中的
<style name="Widget.CalendarView">
<item name="android:showWeekNumber">true</item>
<item name="android:minDate">01/01/1900</item>
<item name="android:maxDate">12/31/2100</item>
<item name="android:shownWeekCount">6</item>
<item name="android:selectedWeekBackgroundColor">#330099FF</item>
<item name="android:focusedMonthDateColor">#FFFFFFFF</item>
<item name="android:unfocusedMonthDateColor">#66FFFFFF</item>
<item name="android:weekNumberColor">#33FFFFFF</item>
<item name="android:weekSeparatorLineColor">#19FFFFFF</item>
<item name="android:selectedDateVerticalBar">@android:drawable/day_picker_week_view_dayline_holo</item>
<item name="android:weekDayTextAppearance">@android:style/TextAppearance.Small.CalendarViewWeekDayView</item>
<item name="android:dateTextAppearance">?android:attr/textAppearanceSmall</item>
</style>
指定了CalendarView 的atrrs 中的一些默认值。
自此我们明白了defStyleAttr的用法。 如果某个stylable 定义了一些item , 但是使用者并不一定对所有的item 在xml 使用时设置。 我们需要给他设定一些默认值
这些值可以在不同的theme 中不一样。
defStyleRes 使用则更简单。可以直接某个style, 当defStyleAttr不起作用时。
相关推荐
Android自定义Attr属性 的具体使用例子,看完之后,你就知道如何自定义控件的属性
最近项目中经常需要用到自定义控件,因此自定义属性也是经常要用到的,在此说明一下自定义属性的用法: 自定义属性都存在于/value/attr.xml文件中,以如下格式存在。 代码如下: ”自定义属性名称”> <attr name=”...
Android自定义控件属性详细介绍 1. reference:参考某一资源ID。 (1)属性定义: <attr xss=removed xss=removed> (2)属性使用: <ImageView android:layout_width = 42dip android:layout_...
在Android开发中,往往要用到自定义的控件来实现我们的需求或效果。在使用自定义 控件时,难免要用到自定义属性,那怎么使用自定义属性呢? 在文件res/values/下新建attrs.xml属性文件,中定义我们所需要的属性。 ...
测试:Android 中自定义属性(attr.xml,TypedArray)的使用 注意:MyView(Context context,AttributeSet attrs)构造函数的实现, 和注意main.xml的LinearLayout 里加的声明 要了解:test:textSize="20px" test:...
option自定义属性的用处,可以推广到其他的html标签
可以自定义的属性有: <!-- 滑动解锁控件 xml配置属性 --> <attr name=slideImageViewWidth format=dimension/><!-- 滑块宽度 --> <attr name=slideImageViewResId format=reference/><!-- 滑块...
大家也可以结合《理解Android中的自定义属性》这篇文章进行学习,后续一篇还有应用。 1、attrs文件编写 <?xml version=1.0 encoding=utf-8?> <resources> <attr name=titleText format=string> <attr ...
NULL 博文链接:https://elingwange.iteye.com/blog/1285289
Android原生控件只有横向进度条一种,而且没法变换样式,比如原生rom的样子 很丑是吧,当伟大的产品设计要求更换前背景,甚至纵向,甚至圆弧状的,咋办,比如: ok,我们开始吧: 一)变换前背景 先来看看...
在上一篇的文章中介绍了自定义控件的属性,详情见《详解Android自定义控件属性TypedArray以及attrs》。那么在这基础上实现随机验证码生成,里面的代码是自定义控件以及涉及到自定义view绘画。 1、先看实现的效果图 ...
在jQuery的attr与prop提到过在IE9之前版本中如果使用property不当会造成内存泄露问题,而且关于Attribute和Property的区别也让人十分头痛,在HTML5中添加了data-*的方式来自定义属性,所谓data-*实际上上就是data-...
开发中碰到个需求,需要在一个空间中选择完成开始和结束时间。实现的过程走的是程序员开发的老路子,找到轮子后自己改吧改吧就成了。...1.自定义控件属性 <attr name=dateformat format=string></attr
本文实例为大家分享了Android自定义星星可滑动评分控件的具体方法,供大家参考,具体内容如下 此控件通过线性布局结合ImageView来实现。 具有展示分数,滑动评分功能,可设置0-10分,自行设置星星图片,是否可点击...
然而在Android系统中自带的打分控件,RatingBar特别不好用,间距和大小无法改变。所以,我就自定义了一个特别好用的打分控件。在项目中可以直接使用,特别简单。下面直接上图: 效果图 实现原理 其实就是自定义View...
本文实例分析了Android重写View并自定义属性的方法。分享给大家供大家参考,具体如下: 这里通过自定义属性 实现如下图所示效果: 第一步:在res\values的目录下新建一个文件attrs.xml 声明一些自定义属性 <?...
本文实例为大家分享了Android自定义圆环倒计时控件的具体代码,供大家参考,具体内容如下 先来一张最终效果图: 主要思路: 在画渐变色圆环的时候,设置一个属性动画,根据属性动画的执行时长,来作为倒计时的时长.监听...
最近接触了Android自定义控件,涉及到自定义xml中得属性(attribute),其实也很简单,但是写着写着,发现代码不完美了,就是在attrs.xml这个文件中,发现属性冗余,于是就想有没有类似属性继承或者include之类的方法....
通过这个方法可以方便地从第一个匹配元素中获取一个属性的值。如果元素没有相应属性,则返回 undefined ) 2. attr(属性名, 属性值) //设置属性的值 (为所有匹配的元素设置一个属性值。) 3. attr(属性名,函数值) /...
本文实例为大家分享了Android自定义控件ViewGroup实现标签云的具体代码,供大家参考,具体内容如下 实现的功能: 基本绘制流程: 构造函数获取自定义属性 onMeasure()方法,测量子控件的大小 onLayout()方法,...