android studio 中不同的 activity 对相同的项目使用不同的样式(请阅读说明)

Use different style for same item for different activity in android studio (Please read description)

我正在做一个项目,我想在不同的活动中使用 BottomSheetDialog。但是对于不同的活动,我希望对话框具有不同的可绘制背景。我已经在 themes.xml 中设置了背景可绘制对象,但这使得可绘制对象适用于整个应用程序的 BottomSheetDialog,即它已成功应用,但对于所有活动都是不变的。我想要的是对于不同的活动,它必须具有不同的可绘制背景。

这是我的 themes.xml 的样子:

<resources xmlns:tools="http://schemas.android.com/tools">
   <style>
    <!-- Base application theme. -->
         ...
         ...
        <!-- Customize your theme here. -->

        <item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>

    </style>

    <style name="AppBottomSheetDialogTheme"
        parent="Theme.Design.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/AppModalStyle</item>
    </style>

    <style name="AppModalStyle"
        parent="Widget.Design.BottomSheet.Modal">
        <item name="android:background">@drawable/top_rounded_corners</item>  <!-- I want to change the backgroundDrawable here. -->
    </style>
</resources>

有没有一种方法可以根据我所在的 activity 更改可绘制对象。我想过使用 AndroidManifest.xml 但我不确定如何实现。 任何帮助表示赞赏。谢谢。

我找到了另一种方法来解决我的问题:我在 themes.xml 中创建了另一个主题实例,然后更改了该实例中的可绘制资源(参见下面的代码)。

<style name="Theme.Blogaro" 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/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->

        <item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>

    </style>

    <style name="Theme.Blogaro.splash" 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/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->

        <item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme.splash</item>  ---> Made changes here

    </style>

    <style name="AppBottomSheetDialogTheme"
        parent="Theme.Design.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/AppModalStyle</item>
    </style>

    <style name="AppBottomSheetDialogTheme.splash"
        parent="Theme.Design.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/AppModalStyle2</item>
    </style>

    <style name="AppModalStyle"
        parent="Widget.Design.BottomSheet.Modal">
        <item name="android:background">@drawable/top_rounded_corners</item>
    </style>

    <style name="AppModalStyle2"
        parent="Widget.Design.BottomSheet.Modal">
        <item name="android:background">@drawable/top_rounded_corners_splash</item>
    </style>

然后我以编程方式更改 Activity.javaonCreate 内的默认主题:

        super.onCreate(savedInstanceState);
        setTheme(R.style.Theme_Blogaro_splash);
        setContentView(R.layout.activity_splash_screen);