样式无法解析

Abd*_*tou 5 xml android android-custom-view styleable declare-styleable

这是我正在使用的代码:

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();
       }
}
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

这些问题的答案无法解决问题。请不要认为我的问题是重复的。


更新:似乎我导入了错误的R类。它R不是应用程序的类android.R

Tin*_*Luu 5

检查您的进口:

  • 错误的:Android.R
  • 正确的:com.example.yourproject.R

在制作这个自定义视图时,我遇到了同样的错误。也许当按照指导步骤操作时,帮助工具会自动插入这个错误的导入。


Abd*_*tou 2

看来我导入了错误的 R 类。它应该是应用程序的 R 类而不是 android.R