以编程方式更改启动活动样式

我有一个活动 (MainActivity.java) 用作启动屏幕,同时加载主要片段并且其他功能在后台发生。此启动屏幕始终显示棕色瓷砖背景和图标。我想要的是仅当变量 dayMode 为 false(Constants.java 中的变量)时显示背景(在 R.style.AppTheme_NoActionBar_LauncherNight 中)。否则,背景应该是 R.style.AppTheme_NoActionBar_LauncherDay 中的背景(白色背景和相同的图标)。


如果我在我的清单的 android:theme 部分指定一个或另一个背景,它会很好地显示。但我想要的是以编程方式设置一个或另一个主题,具体取决于 dayMode 的值,在活动的 onCreate 方法上。这是行不通的。


正如我在其他答案中读到的那样,我在调用 super.onCreate 或 setContentView 之前尝试使用 setTheme,但它不起作用。我只找到解释调用 setTheme 和 setContentView 的顺序的答案,但它们并没有解决这个问题。


我的风格:


 <style name="AppTheme" parent="Theme.AppCompat.Light">

        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/colorPrimary</item>

        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

        <item name="colorAccent">@color/colorAccent</item>

        <item name="autoCompleteTextViewStyle">@style/cursorColor</item>

        <item name="android:textColorSecondary">@color/yellow_light</item>

 </style>


 <style name="AppTheme.NoActionBar.LauncherNight">

        <item name="android:windowBackground">@drawable/launch_screen</item>

 </style>


 <style name="AppTheme.NoActionBar.LauncherDay">

        <item name="android:windowBackground">@drawable/launch_screen_day</item>

 </style>

我的清单:


    <activity

            android:name="com.AlbaRam.myApp.MainActivity"

            android:configChanges="keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

            android:label="@string/app_name"

            android:theme="@style/AppTheme.NoActionBar.LauncherNight"

            android:launchMode="singleInstance"

            android:windowSoftInputMode="stateAlwaysHidden">


            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>


        </activity>



呼唤远方
浏览 137回答 3
3回答

白猪掌柜的

经过一些研究,我发现这是不可能的。当我们想要在加载主要功能时显示某些内容时使用启动屏幕,因此我们将要显示的可绘制对象包含在清单中,以便在我们的主要活动加载时快速显示。这个启动屏幕,就像它在 Manifest 中一样,出现在其他任何东西之前,所以如果我们可以动态地改变启动屏幕的主题,我们将在加载其他所有内容时失去快速出现。

Cats萌萌

public void onCreate(Bundle savedInstanceState) {&nbsp;if (Constants.dayMode){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setTheme(android.R.style.yourTheme);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setTheme(android.R.style.yourTheme);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.actvity);}

肥皂起泡泡

试试这个代码主题.xml<resources><style name="AppThemeLight" parent="Theme.AppCompat.Light">&nbsp; &nbsp; <!-- Customize your theme here. -->&nbsp; &nbsp; <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item></style><style name="AppThemeDark" parent="Theme.AppCompat">&nbsp; &nbsp; <!-- Customize your theme here. -->&nbsp; &nbsp; <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item></style><!-- This will set the fade in animation on all your activities by default --><style name="WindowAnimationTransition">&nbsp; &nbsp; <item name="android:windowEnterAnimation">@android:anim/fade_in</item>&nbsp; &nbsp; <item name="android:windowExitAnimation">@android:anim/fade_out</item></style>活动&nbsp; @Override&nbsp; &nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; AppSettings settings = AppSettings.getInstance(this);&nbsp; &nbsp; &nbsp; &nbsp; setTheme(settings.getBoolean(AppSettings.Key.USE_DARK_THEME) ? R.style.AppThemeDark : R.style.AppThemeLight);&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_transition_theme);&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java