首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android开发自定义控件有关问题,无法读取属性值

2013-04-12 
Android开发自定义控件问题,无法读取属性值values下定义一个attrs.xml文件:?xml version1.0 encoding

Android开发自定义控件问题,无法读取属性值
values下定义一个attrs.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="duration">
</attr>
<declare-styleable name="AlphaImageView">
<attr name="duration"/>
</declare-styleable>
</resources>
-----------------------------------------------------
在主界面中定义了AlphaImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:crazyit="http://schemas.android.com/apk/res/org.crazyit.res"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<org.crazyit.res.AlphaImageView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:src="@drawable/ic_launcher"
crazyit:duration="60000"<!-- 将其属性值设为60000 -->
/>
</LinearLayout>
---------------------------------------------------------
可是在AlphaImageView 的构造函数中,无法取出duration的值,代码如下:
public AlphaImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.AlphaImageView);
//获取duration参数
int duration = typedArray.getInt(
R.styleable.AlphaImageView_duration , 0);
//计算图像透明度每次改变的大小
alphaDelta = 255 * SPEED / duration;//duration值为0,不知何原因
}
----------------------------------------------------------
不知是没有存入属性值,还是取值错误,请各位大侠帮帮忙,谢谢
Android开发 控件
[解决办法]
<attr name="duration"/>
改成
<attr name="duration" format="integer"/>
试试

热点排行