对在values文件下的attr.xml的解读:对属性格式的要求
view plaincopy to clipboardprint?
<declare-styleable name="Favorite">
<attr name="className" format="string" />
<attr name="packageName" format="string" />
<attr name="screen" format="string" />
<attr name="x" format="string" />
<attr name="y" format="string" />
</declare-styleable>
<declare-styleable name="Favorite">
<attr name="className" format="string" />
<attr name="packageName" format="string" />
<attr name="screen" format="string" />
<attr name="x" format="string" />
<attr name="y" format="string" />
</declare-styleable>
声明自己的属性:<declare-styleable name="Favorite">--->定义自己风格的名称
<attr name="animationDuration" format="integer" />
定义自己风格中的属性 比如animationDuration--->对格式的要求
比如我在xml文件夹中加入 judy.xml
xml的具体内容如下:
view plaincopy to clipboardprint?
<favorites xmlns:judy="http://schemas.android.com/apk/res/com.android.launcher">
lt;favorite
judy:packageName="com.android.vending"
judy:className="com.android.vending.AssetBrowserActivity"
judy:screen="1"
judy:x="3"
judy:y="2" />
<favorites/>
<favorites xmlns:judy="http://schemas.android.com/apk/res/com.android.launcher">
<favorite
judy:packageName="com.android.vending"
judy:className="com.android.vending.AssetBrowserActivity"
judy:screen="1"
judy:x="3"
judy:y="2" />
<favorites/>
还可以通过attr 定义属性 然后再xml中引用。引用时候要加上
注意点:xmlns:judy="http://schemas.android.com/apk/res/自己的包名--->在家定义的
xmlns:android="http://schemas.android.com/apk/res/android--->引用android系统的命名空间下的属性
那样在自己的 layout 中的xml中就可以在judy空间中--->用到在attr中定义的属性
view plaincopy to clipboardprint?
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Panel);
mDuration = a.getInteger(R.styleable.Panel_animationDuration, 750); // duration defaults to 750 ms
mPosition = a.getInteger(R.styleable.Panel_position, BOTTOM); // position defaults to BOTTOM
mLinearFlying = a.getBoolean(R.styleable.Panel_linearFlying, false); // linearFlying defaults to false
mOpenedHandle = a.getDrawable(R.styleable.Panel_openedHandle);
mClosedHandle = a.getDrawable(R.styleable.Panel_closedHandle);
a.recycle();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Panel);
mDuration = a.getInteger(R.styleable.Panel_animationDuration, 750); // duration defaults to 750 ms
mPosition = a.getInteger(R.styleable.Panel_position, BOTTOM); // position defaults to BOTTOM
mLinearFlying = a.getBoolean(R.styleable.Panel_linearFlying, false); // linearFlying defaults to false
mOpenedHandle = a.getDrawable(R.styleable.Panel_openedHandle);
mClosedHandle = a.getDrawable(R.styleable.Panel_closedHandle);
a.recycle();
通过在attr中定义的属性 在类中应用时候,先通过TypedArray a=obtainStyledAttributes(attrs, R.styleable.Panel);//得到该定义的属性
然后获取对应的属性 比如
mDuration = a.getInteger(R.styleable.Panel_animationDuration, 750); // duration defaults to 750 ms
因为在attr中定义的animationDuration 为Integer格式。所有这边能赋值750。。
一般这样的语句都是初始化的工作。。
a.recycle();//让初始化生效。
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/wong_judy/archive/2010/03/12/5373476.aspx