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

java–Android– 如何以编程方式创建FAB?,第1张

概述我有一个充满自定义视图的应用程序.当我尝试以编程方式创建FAB时,它会抛出错误Causedby:java.lang.IllegalArgumentException:YouneedtouseaTheme.AppCompattheme(ordescendant)withthedesignlibrary.这是我的代码.privateFloatingActionButtongetFAB(){

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

以上是内存溢出为你收集整理的java – Android – 如何以编程方式创建FAB?全部内容,希望文章能够帮你解决java – Android – 如何以编程方式创建FAB?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1097087.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存