【问题标题】:Android change windowBackground programmatically on application startAndroid在应用程序启动时以编程方式更改windowBackground
【发布时间】:2016-10-18 03:32:31
【问题描述】:

我很清楚如何使用 manifest 中的主题和标签 windowBackground 在 Android 中设置活动的 splash

最近出现了一位客户,要求“根据白天的某些事件更改初始屏幕”。 我几乎可以肯定它无法完成,但我决定用这段代码试一试:

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

显然这是行不通的。

有人已经尝试过,或者有更好的主意?

谢谢

编辑:

创建一个虚假的活动或片段以显示为启动屏幕很容易,但会在应用程序启动时留下令人不快的白色(或黑色,取决于主题)闪烁。

这个问题是关于以编程方式更改初始屏幕的可行性,其结果与在清单中硬编码相同。

【问题讨论】:

  • 你能做到吗?
  • 抱歉,好像不可能。您可以在您的应用程序启动时设置启动画面,但它是不可更改的,如下所述:bignerdranch.com/blog/splash-screens-the-right-way 或者您可以创建一个类似的视图,如果您在打开应用程序时可以承受半秒的空白屏幕,您可以随时更改。

标签: android splash-screen


【解决方案1】:

如果您删除 Splash 并通过创建一个全屏显示您想要的图像 n 秒的 Activity 来模拟它怎么办?

【讨论】:

    【解决方案2】:

    如果您只想更改背景,请尝试此操作,操作栏颜色和外观可以以几乎相同的方式完成。

        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);
            }
        }
    

    这应该适用于当前活动。

    【讨论】:

    • 正如我在 EDIT 中所说的,这不是我期待的初始屏幕类型。
    【解决方案3】:

    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);
        }
    }
    

    或 Kotlin:

    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 中决定的状态),而我们进行我们的应用初始化。

    这并不完美,但对我们来说已经足够了——也许它可以帮助其他人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多