首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用FragmentScenario时的样式问题

使用FragmentScenario时的样式问题
EN

Stack Overflow用户
提问于 2021-02-21 08:30:55
回答 1查看 282关注 0票数 1

当使用FragmentScenarioandroidx.fragment:fragment-testing测试片段UI时,并不是所有的样式都被正确地应用。

例如,有一个非常简单的应用程序,它只提供具有以下布局的片段:

fragment_main.xml

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

使用Android生成的默认主题(4.1.2)在运行应用程序时如下所示:

作为FragmentScenario测试运行时,情况不一样:

我可以理解没有显示ActionBar (因为它是主机Activity的一部分,而不是Fragment)。但是为什么没有正确地定义. ??

EN

回答 1

Stack Overflow用户

发布于 2022-07-28 11:50:03

正如其他人所评论的那样,默认情况下,FragmentScenario将在EmptyFragmentActivity (在FragmentScenario类中定义)中启动Fragment,这将使用R.style.FragmentScenarioEmptyFragmentActivityTheme

为了确保测试使用正确的主题,我们将主题Theme_App提供给themeResId param (以及任何参数),并正确呈现。

代码语言:javascript
运行
复制
val scenario: FragmentScenario<MyFragment> = launchFragmentInContainer(
            themeResId = style.Theme_App,
            fragmentArgs = args
        )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66300444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档