最快在应用程序启动时更改主题的方法 [英] Fastest way to change theme during app startup

查看:120
本文介绍了最快在应用程序启动时更改主题的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我确实提供2主题在我的应用程序,根据用户的上上之选 - 黑暗的主题和光的主题

Currently, I do provide 2 theme in my app, based on user last choice - dark theme and light theme.

在主要活动启动时,我会做到以下几点。

During main activity start-up, I will do the following.

public class MyFragmentActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // getUserLastThemeChoice will either return R.style.Theme_My_Light or 
        // R.style.Theme_My_Dark.
        this.setTheme(getUserLastThemeChoice());

        super.onCreate(savedInstanceState);

在我的的Andr​​oidManifest.xml ,我有以下

<application
    ...
    android:theme="@style/Theme.My.Dark" >

由于的Andr​​oidManifest.xml 的硬codeD值,如果用户previous选择是光的主题,我会看到以下内容。

Due to the hard-coded value in AndroidManifest.xml, if user previous choice is light theme, I will observed the following.

  1. 在应用程序启动与黑暗的主题,因为的Andr​​oidManifest.xml
  2. 在一段时间...
  3. 应用会变成白色的主题,由于 this.setTheme 活动
  1. App start up with dark theme, due to AndroidManifest.xml
  2. A while...
  3. App will change to white theme due to this.setTheme in main Activity

在我有什么办法能避免这种转型的观察?就是这样,只要present用户直接根据他们最后的选择。

In there any way I can avoid such transition observation? That's it, just present to user directly, based on their last choice.

P / S注,那就没有多大帮助,如果您从清单中的主题信息。由于系统会默认使用全息暗。你仍然会看到过渡,特别是在慢速设备。

p/s Note, it wouldn't help much if you remove the theme information from manifest. As the system is going to use holo dark by default. You will still observe the transition, especially in slow device.

推荐答案

隐藏主题操纵,特别是在应用程序启动是棘手的。不幸的是没有办法统一删除,最初的过渡,因为没有告诉有多快/慢的设备上运行它。 Android将永远膨胀的布局为主要活动之前显示的默认主题的空白背景。

Hiding theme manipulation, particularly on app start up is tricky. Unfortunately there is no way to consistently remove that initial transition, because there's no telling how fast/slow the device running it is. Android will always show that blank background in the default theme before inflating the layout for the main activity.

但有一招隐藏它。当用户打开应用程序,并淡出了最初的活动已加载后会出现一个启动画面。这样,闪屏隐藏主题过渡,没有刺耳的用户。闪屏接收一些当之无愧的恨,但这样的情况并证明其使用。同样,我不知道您的应用程序的细节,但你必须权衡有一个闪屏和平稳过渡,对不闪屏和不和谐过渡的优点/缺点。

But there is a trick to hiding it. A splash screen that appears when the user opens the app, and fades out after the initial activity has loaded. This way the splash screen hides the theme transition, without jarring the user. Splash screens receive some well deserved hate, but a situation like this does justify their use. Again, I don't know the specifics of your app, but you have to weigh the pros/cons of having a splash screen and smooth transition vs. no splash screen and a jarring transition.

有在实施闪屏的方式加载。你的情况我会尝试创建一个启动活动,用中性的主题,并应用流畅的动画开机画面和你的主要活动,像这样的转变:

There are loads of ways of implementing splash screens. In your case I would try to create a launcher activity, with a neutral theme, and apply a smooth animation for the transition between the splash screen and your main activity like so:

startActivity( new Intent( LauncherActivity.this, MainActivity.class ) );
overridePendingTransition( R.anim.fade_in, R.anim.fade_out );

和动画:

<!-- fadeout -->
<?xml version="1.0" encoding="UTF-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="300" />

<!-- fadein -->
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="300" />

这篇关于最快在应用程序启动时更改主题的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆