在 xml android 中无法访问自定义视图属性
Custom View attributes are not accessible in xml android
我正在尝试在我的 CustomView 的 xml 中设置 drawable。但它没有显示我的 app:enableProgressRes="@drawable/vbequalizer_bg"
或 customname:enableProgressRes="@drawable/vbequalizer_bg"
我的意思是不包括自定义属性。当我们点击 Ctrl+Space
android:attr 显示。
And i have tried including these namespaces one by one to the Root Layout.
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"
But my attributes aren't accessible in xml. by anyOne of them
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();
}
}
}
可绘制资源的正确格式是 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>
我正在尝试在我的 CustomView 的 xml 中设置 drawable。但它没有显示我的 app:enableProgressRes="@drawable/vbequalizer_bg"
或 customname:enableProgressRes="@drawable/vbequalizer_bg"
我的意思是不包括自定义属性。当我们点击 Ctrl+Space
android:attr 显示。
And i have tried including these namespaces one by one to the Root Layout.
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"
But my attributes aren't accessible in xml. by anyOne of them
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();
}
}
}
可绘制资源的正确格式是 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>