2017-04-16 92 views
1

我试图使用程序兼容性DayNight主题在我的Android Wear应用,但它不工作,我的活动需要的环境模式,所以我延长WearableActivity这样的:Android Wear DayNight主题程序兼容性

public class BaseActivity extends WearableActivity { 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setAmbientEnabled(); 
     .... 
    } 

} 

对于我的主题我碰到这样的:

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar"> 
     <item name="android:windowNoTitle">true</item> 
     <item name="android:windowBackground">@color/colorBackground</item> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="android:textColorPrimary">@color/textColorPrimary</item> 
    </style> 

但没有什么工作,主题不会改变在所有...我用同样的主题在我的手机应用程序和它的作品,唯一的区别是,我的活动延伸AppCompatActivity。

有没有办法使它适用于Android Wear应用程序?

+0

莫非当您在可穿戴设备上运行应用程序时,您还会分享您的应用程序会发生什么情况?控制台中出现任何错误?或者,您可能想尝试使用[tutorial](https://medium.com/@chrisbanes/appcompat-v23-2-daynight-d10f90c83e94)中提到的AppCompatDelegate.setDefaultNightMode(),并查看它是否可以工作为你。有关更多见解,您可能还需要访问[此博客](https://android-developers.googleblog.com/2016/02/android-support-library-232.html)。 – Teyam

+0

当我运行设备时,没有附加任何东西,即使我使用setDefaultNightMode强制使用该设备,我只需看看AppCompatActivity的源代码,并在其中添加一些代码,以便在需要时应用正确的主题,我将尝试将该代码复制/粘贴到WearableActivity中,看看它是否有效 – jaumard

回答

0

我管理,使之成为我的使用情况下的工作,加入这个我WearableActivity(从AppCompatActivity复制/粘贴)(强迫白天或晚上):

public class BaseActivity extends WearableActivity implements AppCompatCallback { 
    private AppCompatDelegate delegate; 
    private int themeId = 0; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 

     final AppCompatDelegate delegate = getDelegate(); 
     delegate.installViewFactory(); 
     delegate.onCreate(savedInstanceState); 
     if (delegate.applyDayNight() && themeId != 0) { 
      // If DayNight has been applied, we need to re-apply the theme for 
      // the changes to take effect. On API 23+, we should bypass 
      // setTheme(), which will no-op if the theme ID is identical to the 
      // current theme ID. 
      if (Build.VERSION.SDK_INT >= 23) { 
       onApplyThemeResource(getTheme(), themeId, false); 
      } else { 
       setTheme(themeId); 
      } 
     } 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public void setTheme(@StyleRes final int resid) { 
     super.setTheme(resid); 
     // Keep hold of the theme id so that we can re-set it later if needed 
     themeId = resid; 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     getDelegate().onSaveInstanceState(outState); 
    } 

    /** 
    * @return The {@link AppCompatDelegate} being used by this Activity. 
    */ 
    @NonNull 
    public AppCompatDelegate getDelegate() { 
     if (delegate == null) { 
      delegate = AppCompatDelegate.create(this, this); 
     } 
     return delegate; 
    } 

    @Override 
    public void onSupportActionModeStarted(ActionMode mode) { 

    } 

    @Override 
    public void onSupportActionModeFinished(ActionMode mode) { 

    } 

    @Nullable 
    @Override 
    public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) { 
     return null; 
    } 
} 

现在我可以用AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES/NO);