为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

呼唤远方 2023-03-09 10:55:32
我有一个活动 (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>
查看完整描述

3 回答

?
白猪掌柜的

TA贡献1893条经验 获得超10个赞

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



查看完整回答
反对 回复 2023-03-09
?
Cats萌萌

TA贡献1805条经验 获得超9个赞

public void onCreate(Bundle savedInstanceState) {


 if (Constants.dayMode){


           setTheme(android.R.style.yourTheme);


        } else {


           setTheme(android.R.style.yourTheme);


        }

    super.onCreate(savedInstanceState);


    setContentView(R.layout.actvity);

}


查看完整回答
反对 回复 2023-03-09
?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

试试这个代码


主题.xml


<resources>

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

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

    <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>

</style>

<style name="AppThemeDark" parent="Theme.AppCompat">

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

    <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>

</style>

<!-- This will set the fade in animation on all your activities by default -->

<style name="WindowAnimationTransition">

    <item name="android:windowEnterAnimation">@android:anim/fade_in</item>

    <item name="android:windowExitAnimation">@android:anim/fade_out</item>

</style>

活动


  @Override

      protected void onCreate(Bundle savedInstanceState) {

        AppSettings settings = AppSettings.getInstance(this);

        setTheme(settings.getBoolean(AppSettings.Key.USE_DARK_THEME) ? R.style.AppThemeDark : R.style.AppThemeLight);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_transition_theme);


        //


        }


查看完整回答
反对 回复 2023-03-09
  • 3 回答
  • 0 关注
  • 136 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号