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

loader 2013-04-10 11:41:12
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,不知何原因
}
----------------------------------------------------------------------------------------------
不知是没有存入属性值,还是取值错误,请各位大侠帮帮忙,谢谢
...全文
4678 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
longer262110 2013-04-10
  • 打赏
  • 举报
回复
引用 11 楼 loader 的回复:
可以了,不知道什么原因,崩溃了,谢谢兄弟~
不客气,在帮助你们的同时也在提升我自己,呵呵!
loader 2013-04-10
  • 打赏
  • 举报
回复
可以了,不知道什么原因,崩溃了,谢谢兄弟~
longer262110 2013-04-10
  • 打赏
  • 举报
回复
引用 9 楼 loader 的回复:
alphaDelta = 255 * SPEED / duration;//这里是66行,duration值为0了
我表示关注ing
loader 2013-04-10
  • 打赏
  • 举报
回复
alphaDelta = 255 * SPEED / duration;//这里是66行,duration值为0了
longer262110 2013-04-10
  • 打赏
  • 举报
回复
还有你这个地方 <attr name="duration"> </attr> 是多余的
longer262110 2013-04-10
  • 打赏
  • 举报
回复
(AlphaImageView.java:66) 66行是哪里
longer262110 2013-04-10
  • 打赏
  • 举报
回复
引用 4 楼 loader 的回复:
typedArray.recycle(); 也加了,不好使 typedArray.getInt( R.styleable.AlphaImageView_duration , 0); 取出来的值是0,也就是缺省的值,不知道为什么
那就不清楚了,有木有报异常
loader 2013-04-10
  • 打赏
  • 举报
回复
The following classes could not be instantiated: - org.crazyit.res.AlphaImageView (Open Class, Show Error Log) See the Error Log (Window > Show View) for more details. Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse java.lang.ArithmeticException: / by zero at org.crazyit.res.AlphaImageView.<init>(AlphaImageView.java:66) at sun.reflect.NativeConstructorAccessorImpl.newInstance0( at sun.reflect.NativeConstructorAccessorImpl.newInstance( at sun.reflect.DelegatingConstructorAccessorImpl.newInstance( at java.lang.reflect.Constructor.newInstance( at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:422) at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:179) at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207) at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:746) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64) at android.view.LayoutInflater.rInflate(LayoutInflater.java:718) at android.view.LayoutInflater.inflate(LayoutInflater.java:489) at android.view.LayoutInflater.inflate(LayoutInflater.java:372) //以上是XML编辑界面时提示的错误,应该是duration值为0提示的错误
loader 2013-04-10
  • 打赏
  • 举报
回复
typedArray.recycle(); 也加了,不好使 typedArray.getInt( R.styleable.AlphaImageView_duration , 0); 取出来的值是0,也就是缺省的值,不知道为什么
loader 2013-04-10
  • 打赏
  • 举报
回复
改了,还是不行
longer262110 2013-04-10
  • 打赏
  • 举报
回复
引用 1 楼 longer262110 的回复:
<attr name="duration"/> 改成 <attr name="duration" format="integer"/> 试试
并且要加typedArray.recycle();
longer262110 2013-04-10
  • 打赏
  • 举报
回复
<attr name="duration"/> 改成 <attr name="duration" format="integer"/> 试试

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧