2017-04-16 114 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);