本文介绍了Android - 如何以编程方式创建FAB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个充满自定义视图的应用。当我尝试以编程方式创建FAB时,它会抛出错误

I have an app with full of custom views. When I try to create a FAB programmatically, it throws an error

这是我的代码。

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

Fixed by using theme wrapper. But still I'm surprised about using 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 - 如何以编程方式创建FAB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:45