簡體   English   中英

Android 在應用程序啟動時以編程方式更改 windowBackground

[英]Android change windowBackground programmatically on application start

我很清楚如何使用清單中的主題和標簽windowBackground在 Android 中設置活動的啟動

一位客戶最近出現要求“根據白天的某些事件更改啟動畫面”。 我幾乎可以肯定它無法完成,但我決定用以下代碼試一試:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        int random = (int) Math.abs(System.currentTimeMillis() % 3);

        switch (random) {

            case 0:
                setTheme(R.style.FullscreenTheme1);
                break;

            case 1:
                setTheme(R.style.FullscreenTheme2);
                break;

            default:
                setTheme(R.style.FullscreenTheme3);
                break;
        }
    }
}

顯然這是行不通的。

有人已經嘗試過這個或有更好的主意?

謝謝

編輯:

創建一個虛假的活動或片段以顯示為啟動畫面很容易,但會在應用程序啟動時留下令人不快的白色(或黑色,取決於主題)閃爍。

這個問題是關於以編程方式更改啟動畫面的可行性,其結果與在清單中對其進行硬編碼。

如果您刪除 Splash 並通過在全屏模式下創建一個 Activity 來模擬它,該 Activity 會顯示您想要的圖像 n 秒?

選擇主題的更好/更早的地方是onApplyThemeResource (在onCreate之前調用):

public class MyApplication extends Application {

    @Override
    protected void onApplyThemeResource(Resources.Theme theme, int resid,
        boolean first) {
        int random = (int) Math.abs(System.currentTimeMillis() % 3);

        int myRes;
        switch (random) {
            case 0:
                myRes = R.style.FullscreenTheme1;
                break;

            case 1:
                myRes = R.style.FullscreenTheme2;
                break;

            default:
                myRes = R.style.FullscreenTheme3;
                break;
        }
        super.onApplyThemeResource(theme, myRes, first);
    }
}

或科特林:

override fun onApplyThemeResource(theme: Resources.Theme, resid: Int, first: Boolean) {
    val myRes = when(System.currentTimeMillis() % 3) {
        0L -> R.style.FullscreenTheme1
        1L -> R.style.FullscreenTheme2
        else -> R.style.FullscreenTheme3
    }
    super.onApplyThemeResource(theme, myRes, first)
}

但是,這並不能解決應用程序啟動時白/黑閃爍的問題。

我們解決這個問題的方法是在AndroidManifest.xml設置一個中性主題(只有背景顏色,與所有變體中的主題背景相同)作為android:theme ,然后在onApplyThemeResource切換到正確的變體,在我們的案例只是彩色背景上的一些標志。

結果:應用程序“閃爍”純色背景(而不是黑色/白色,直到 Android 初始化應用程序),然后在我們進行應用程序初始化時在此背景上顯示徽標(取決於onApplyThemeResource決定的狀態)。

這並不完美,但對我們來說已經足夠了 - 也許它可以幫助其他人。

如果您只想更改背景,請嘗試此操作,操作欄的顏色和外觀可以以幾乎相同的方式完成。

    package com.example;

    import java.util.ArrayList;

    public class WelcomeFragment extends Fragment {
        private LinearLayout mainLayout;

        //since you seem only interested in the background color changing, using a theme might seem to be overkill
        //that is if its for a very short time
        //but then a theme is better if you are concerned about branding
        //what i have here is basically to cahnge the background color
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_welcome, container, false);
mainLayout = (LinearLayout) v.findViewById(R.id.main_layout);
            colorList.add(R.color.accent);
            colorList.add(R.color.primary_light);
            colorList.add(R.color.secondary_light);
            colorList.add(R.color.secondary);
            colorList.add(R.color.secondary_dark);
            colorList.add(R.color.colorPrimaryDark);
            colorList.add(R.color.primary_dark);
            colorList.add(R.color.primary);

            return v;
        }

        ArrayList<Integer> colorList = new ArrayList<>();

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
           new CountDownTimer((1000 * 14), 1000) {//set my timer to 14 seconds, restarts if a certain condition is not met
           //set the timing to appropriate values or if you are just interested in a particular event do this in that section
                public void onTick(long millisUntilFinished) {
                    //do update here
                    mainLayout.setBackgroundColor(getResources().
                            getColor(colorList.get((int) (millisUntilFinished / 1000) % 7)));
                }

                public void onFinish() {
                    if (!done)
                        this.start();
                }
            }.start();

            super.onViewCreated(view, savedInstanceState);
        }
    }

這應該適用於當前的活動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM