我有以下两个主题:

<!-- Base application theme. -->
<style name="Theme.MyDemoApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <item name="colorOnSecondary">@color/purple_500</item>
</style>

<!-- Base application theme with a prominent app bar incl. image -->
<style name="Theme.MyDemoApp.Prominent.Image" parent="Theme.MyDemoApp">

    <!-- Transparent status bar, so that the image extents to the status bar-->
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>

主题Theme.MyDemoApp正在我的应用程序或实际上我的整个应用程序中使用.

一个活动打开一个特殊的片段.这个片段使用了一个带有图像的突出应用程序栏,图像也显示在状态栏上. 因此,我创建了一个名为Theme.MyDemoApp.Prominent.Image的主题,它使我能够在状态栏上显示图像.

由于片段将从不同的活动和片段中调用,因此我希望将Theme.MyDemoApp.Prominent.Image设置为片段. 我已经寻找了许多将主题设置为碎片的解决方案,例如:

1)

@Override
public void onAttach(@NonNull Context context) {
    context.setTheme(R.style.Theme_MyDemoApp_Prominent_Image);
    super.onAttach(context);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //create ContextThemeWrapper from the original Activity Context with the custom theme
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.Theme_MyDemoApp_Prominent_Image);

    //clone the inflater using the ContextThemeWrapper
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
    
    return inflater.inflate(R.layout.fragment_demo_appbar_image, container, false);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    getContext().getTheme().applyStyle(R.style.Theme_MyDemoApp_Prominent_Image, true);
    return inflater.inflate(R.layout.fragment_demo_appbar_image, container, false);
}

直接在片段XML文件中设置主题

<androidx.coordinatorlayout.widget.CoordinatorLayout
    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"
    style="@style/Theme.MyDemoApp.Prominent.Image"
    android:theme="@style/Theme.MyDemoApp.Prominent.Image"
    >
<!-- bla bla bla bla -->
</androidx.coordinatorlayout.widget.CoordinatorLayout>

然而,上面try 过的解决方案对我来说都不起作用.主题不会改变,也就是状态栏不透明. 如果我把片断主题参数放到活动主题中,它的工作就像一个护身符,但这不是我计划做的……

问题可能是什么,以及我如何才能正确设置片段中的主题?

推荐答案

试着在2分钟内达到return localInflater.inflate(...分.试着

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //create ContextThemeWrapper from the original Activity Context with the custom theme
    final Context contextThemeWrapper = 
        new ContextThemeWrapper(getActivity(), R.style.Theme_MyDemoApp_Prominent_Image);

    //clone the inflater using the ContextThemeWrapper
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
    return localInflater.inflate(R.layout.fragment_demo_appbar_image, container, false);
}

编辑:现在我更清楚地看到了问题……

您正在try 设置statusBarColorwindowTranslucentNavigation-它们是Activity的参数,而不是Fragment.因此,您的主题应该应用于Activity,在FragmentView层/级别上设置主题(Fragment的子级的种类,这是Activity的子级)通过Context包装或编程设置/通过XML进行设置太低了,主机Activity不会应用这些属性

因此,当Fragment显示时,您必须为整个Activity-半透明状态栏设置Theme,并在分离它时返回默认(黑色?) colored颜色 .但您只能以编程方式将整个ActivityTheme设置为calling setTheme( before super.onCreate( call,所以只有在开始时,当您可能不确定是否会显示半透明状态Fragment时才能这样做.(例如,它可能会在运行时的某些点击时显示)

所以你需要重新启动Activity,然后用一些额外的标志定制setTheme(,你的Fragment也应该弹出,然后在开始...您可以使用Bundle来代替Intent,在finish()调用之后再次启动这Activity

我确实认为这太费力气了,您的Fragment实际上应该是另一个Activity,具有这个由XML设置的透明状态主题,或者它可能是大约transparet-Activity-theme的某个扩展,而不是您的"Theme.MyDemoApp"

Android相关问答推荐

广播接收者意图从服务内设置,而不被其他服务接收

将Any强制转换为Integer将从API返回NullPointerException

无法在Jetpack Compose中显示Admob原生广告

如何消除脚手架和顶杆(material 3)更改 colored颜色 时的延迟?

在命令行Android应用程序开发中苦苦挣扎

Android 11:在try 获取文件的永久权限后,仍然没有读写权限

如何在喷气背包中绕过集装箱

使用lazyColumn迁移paging3的旧代码

如何仅同步 local_manifest.xml?

Android AGP 8 + Gradle 8 + Kotlin 1.8 导致 Kapt 出错

系统导航栏在某些场景下应用了深色效果

React Native Android 应用程序在调试模式下运行良好,但当我们发布 apk 时,它会生成旧版本的应用程序

视觉转换后获取文本

Android:appcompat 和 material 如何从默认创建 appcompat 和 material 视图?

try 使用 ViewPager2 实现滑动视图时出现类型不匹配错误

喷气背包组成影子奇怪的行为

清洁架构中的服务

使用 Jetpack Compose 时,如何以简单的方式在 Color.kt 中定义 colored颜色 ?

在 Compose 中使用 DeepLink 会导致无法向后导航

MVVM - 这个逻辑的最佳层是什么?