java – Android – 如何以编程方式创建FAB?

我有一个充满自定义视图的应用程序.当我尝试以编程方式创建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;
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...