java - 以编程方式更改启动 Activity 样式

标签 java android themes

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

如果我在 list 的 android:theme 部分指定一个或另一个背景,它会很好地显示。但我想要的是根据 dayMode 的值,在 Activity 的 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>

我的 list :

    <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>

我的 Activity 主要:

@Override
    protected void
    onCreate(Bundle savedInstanceState) {
        //This is not working
        if (Constants.dayMode){
            super.setTheme(R.style.AppTheme_NoActionBar_LauncherDay);
        } else {
            setTheme(R.style.AppTheme_NoActionBar_LauncherNight);
        }

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        //rest of functionality

    }

最佳答案

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

关于java - 以编程方式更改启动 Activity 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56885482/

相关文章:

java - ScheduledExecutorService 工作线程在 FutureTask.cancel(true) 之后保持中断状态

java - 使用 Firestore RecyclerView 加载数据导致应用崩溃

java - X.509 证书不完整 : SubjectAlternativeName extension MUST be marked critical when subject field is empty

java - 与串行端口通信时抛出异常

android - 如何通过蓝牙唤醒处于蓝牙待机状态的设备(如 avr Sony STR-DN1080)

android - 如果高度为 WRAP_CONTENT,则 VideoView 不可见

themes - reveal.js 中的代码块使用了哪个主题?

css - jQuery-UI Datepicker CSS问题

css - Wordpress 菜单不会保持在顶部

java - Eclipse RCP - 以编程方式更新菜单项