首页 > 解决方案 > 使用 FragmentScenario 时的样式问题

问题描述

使用FragmentScenariofromandroidx.fragment:fragment-testing测试片段 UI 时,并非所有样式都正确应用。

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

fragment_main.xml

<?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 Studio (4.1.2) 生成的默认主题,在运行应用程序时如下所示:

在此处输入图像描述

作为FragmentScenario测试运行时看起来不一样:

在此处输入图像描述

我可以理解ActionBar没有显示(因为它是 hostActivity而不是 a的一部分Fragment)。但是为什么Button样式不正确...??

标签: androidandroid-fragmentsandroid-testingandroid-fragmentscenario

解决方案


推荐阅读