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,不知何原因
}
----------------------------------------------------------------------------------------------
不知是没有存入属性值,还是取值错误,请各位大侠帮帮忙,谢谢