簡體   English   中英

在 android 工作室中為相同的項目使用不同的樣式(請閱讀說明)

[英]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>

有沒有辦法可以根據我正在進行的活動更改可繪制對象。 我想過使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM