我正在尝试在我的CustomView的xml中设置 drawable 。但它没有显示我的app:enableProgressRes="@drawable/vbequalizer_bg"
或customname:enableProgressRes="@drawable/vbequalizer_bg"
我的意思是不包含自定义属性。当我们点击Ctrl+Space
android:attr shows。
我尝试将这些命名空间逐个包含在根布局中。
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:customname="http://schemas.android.com/apk/res/zare.custom.views"
但我的属性在xml中无法访问。由他们中的任何一个
This answer says在ADT 17之后问题得到解决。
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="VolumnBar">
<attr name="disableProgressRes" format="integer" />
<attr name="enableProgressRes" format="integer" />
</declare-styleable>
</resources>
CustomView(VolumeBar)构造函数
public VolumnBar(Context context, AttributeSet attributeset)
{
super(context, attributeset);
if(!isInEditMode())
{
TypedArray a = context.obtainStyledAttributes(attributeset, R.styleable.VolumnBar);//VolumnBar same name as Class Name
try
{
drawableProgress = a.getDrawable(R.styleable.VolumnBar_disableProgressRes);
drawableInvalidateProgress = a.getDrawable(R.styleable.VolumnBar_enableProgressRes);
}
catch (Exception e)
{
}
finally
{
a.recycle();
}
}
}
答案 0 :(得分:1)
可绘制资源的正确格式为reference
,无integer
。将属性定义更改为以下内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="VolumnBar">
<attr name="disableProgressRes" format="reference" />
<attr name="enableProgressRes" format="reference" />
</declare-styleable>
</resources>