我有一个自定义视图,它也使用自己的defStyleAttr
即
当前主题中的一个属性,它包含对为视图提供默认值的样式资源的引用。
在布局文件中声明此视图并单击Android Studio 2.2.3中的预览时,我收到以下错误消息:
缺少样式。是否为此布局选择了正确的主题?使用布局上方的主题组合框选择不同的布局,或修复主题样式引用。无法找到样式' myCustomStyleAttr'在当前主题
我的视图构造函数是:
public MyCustomLayout(Context context) {
this(context, null);
}
public MyCustomLayout(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.myCustomStyleAttr);
}
public MyCustomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs, defStyleAttr);
}
我知道有很多关于缺少样式的问题。错误但是没有一个答案对我有用,而且它们主要与AppCompat样式有关,所以我想分享我的解决方案。
答案 0 :(得分:1)
以下对我有用:
public MyCustomLayout(Context context) {
this(context, null);
}
public MyCustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, isInEditMode() ? 0 : R.attr.myCustomStyleAttr);
}
public MyCustomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs, defStyleAttr);
}