2017-04-16 17 views
1

私はこのようなWearableActivityを拡張するので、私は私のAndroid WearアプリにAPPCOMPAT白黒モードのテーマを使用しようとしているが、それは働いていない、私の活動は、周囲のモードが必要になります。Android Wearの白黒モードのテーマAPPCOMPAT

public class BaseActivity extends WearableActivity { 

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

} 

私のテーマについてI以下のようなものがあります。

<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

でしたを使用することができますウェアラブルデバイスでアプリを実行したときにアプリで何が起こるかを共有しますか?あなたのコンソールに表示されるエラー?または、この[チュートリアル](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);

関連する問題