亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以編程方式更改啟動活動樣式

以編程方式更改啟動活動樣式

呼喚遠方 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 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號