微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐