简体   繁体   English

在 android 工作室中为相同的项目使用不同的样式(请阅读说明)

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

I am working on a project wherein I want to use BottomSheetDialog in different activities.我正在做一个项目,我想在不同的活动中使用BottomSheetDialog But for different activities I want the Dialog to have diffrent background drawable.但是对于不同的活动,我希望对话框具有不同的背景可绘制。 I have set the background drawable in themes.xml but that makes the drawable to be applicable for BottomSheetDialog for the entire app, ie, it gets applied successfully but it is constant for all activities.我已经在themes.xml中设置了背景可绘制对象,但这使得可绘制对象适用于整个应用程序的BottomSheetDialog ,即它已成功应用,但对于所有活动都是恒定的。 What I want is for diffrent activites it must have different background drawable.我想要的是不同的活动,它必须有不同的背景可绘制。

This is what my themes.xml looks like:这就是我的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>

Is there a way I can change drawable depending on the activity I am on.有没有办法可以根据我正在进行的活动更改可绘制对象。 I thought of using AndroidManifest.xml but I am not sure how can I achieve this.我想过使用AndroidManifest.xml但我不确定如何实现这一点。 Any help is appreciated.任何帮助表示赞赏。 Thank You.谢谢你。

I found an alternative way to solve my problem: I created another instance of themes in themes.xml and then change the drawable asset in this instance (see the code below).我找到了解决我的问题的另一种方法:我在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>

Then I programmatically change the default theme inside the onCreate in my Activity.java :然后我以编程方式更改我的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.

相关问题 Android Studio:在不同的活动中使用“OnClick”方法? - Android Studio: Use “OnClick” Method in different Activity? Android:不同按钮的活动相同,但动作不同 - Android: Same activity for different buttons, but different actions 如何在 Android 中为不同的内容使用相同的 Activity? - How can I use the same Activity for different content in Android? 同一活动中每个微调项的内容不同 - Different content for each Spinner item in the same Activity 使用不同的android开关风格? - Use different android switch style? 在同一活动中使用相同的textView和不同的文本 - Use same textView in same activity with different text 如何在项目点击时重定向到android中的不同Activity - how to redirect to different Activity in android on item click ANDROID:从2个不同的Activity(不同的Intent)开始相同的Activity - ANDROID: Start same Activity from 2 different Activity (different Intent) Android 自定义列表视图在不同的活动上有不同的风格 - Android custom listview has different style on different activity 如何使用 recyclerview 为每个项目打开新活动。 对于每个项目,它应该显示不同的活动。 请帮助我,我是 Android 的新手 - How to open new activity for every item with recyclerview. For every item it should display different activity. Please help me I'm new to Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM