我有一个充满自定义视图的应用程序.当我尝试以编程方式创建FAB时,它会抛出错误
Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
这是我的代码.
private FloatingActionButton getFAB() {
FloatingActionButton fab = new FloatingActionButton(getContext());
fab.setBackgroundDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_add_white_24dp));
return fab;
}
这是我的应用主题.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
请帮我.
解决方法:
通过使用主题包装器修复.但是我仍然对使用ContextThemeWrapper感到惊讶
private FloatingActionButton getFAB() {
Context context = new android.support.v7.internal.view.ContextThemeWrapper(getContext(), R.style.AppTheme);
FloatingActionButton fab = new FloatingActionButton(context);
return fab;
}