[英]How do I create an instance of the TabLayout class programmatically?
I want to create an instance of the TabLayout class programmatically for instrumentation testing purposes.我想以编程方式创建TabLayout class 的实例,用于仪器测试。 Sadly, the TabLayout constructor is throwing an IllegalArgumentException with the following message:可悲的是, TabLayout构造函数抛出IllegalArgumentException并显示以下消息:
The style on this component requires your app theme to be Theme.AppCompat (or a descendant).此组件的样式要求您的应用主题为 Theme.AppCompat(或后代)。
This is what I've tried:这是我尝试过的:
val context = InstrumentationRegistry.getInstrumentation().targetContext
val tabLayout = TabLayout(context)
I've tried TabLayout(context, null, R.style.Theme_AppCompat)
as well but no luck.我也试过TabLayout(context, null, R.style.Theme_AppCompat)
但没有运气。
What do I need to do to create an instance of the TabLayout class when the theme of my context is not Theme.AppCompat
or a descendant of Theme.AppCompat
?当我的上下文主题不是Theme.AppCompat
或 Theme.AppCompat 的后代时,我需要做什么来创建TabLayout Theme.AppCompat
的实例?
What worked for me was to use ContextThemeWrapper as follows:对我有用的是使用ContextThemeWrapper如下:
val context = InstrumentationRegistry.getInstrumentation().targetContext
val tabLayout = TabLayout(ContextThemeWrapper(context, R.style.Theme_AppCompat))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.