这是我使用的代码:
public ASSwitch(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray sharedTypedArray = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.ASSwitch,
0, 0);
try {
onText = sharedTypedArray.getText(R.styleable.ASSwtich_onText, null);
} finally {
sharedTypedArray.recycle();
}
}
这是 attrs.xml 文件(添加到 values 文件夹):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ASSwitch">
<attr name="onText" format="string" />
<attr name="offText" format="string" />
<attr name="onState" format="boolean" />
<attr name="toogleDrawable" format="string" />
<attr name="frameDrawable" format="string" />
</declare-styleable>
</resources>
这些问题的答案无法解决问题。请不要将我的问题视为重复。
更新:看来我导入了错误的 R 类。它应该是应用程序的 R
类而不是 android.R
。
最佳答案
检查你的导入:
- 错误:
Android.R
- 正确:
com.example.yourproject.R
我做这个自定义 View 的时候也出现了同样的错误。也许当按照指导步骤时,帮助工具会自动插入这个错误的导入。
关于android - Styleable无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17242720/