我无法将主题应用于运行时

时间:2020-03-16 10:35:13

标签: android

我创建了两个主题“ AppTheme”和“ BlueTheme”,我想单击按钮来更改主题并保存这些更改 但是我不能!我感到困惑 请帮助我,我是新开发的android studio。

主题

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>



    </style>



    <!--blueTheme-->
    <style name="blueTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="colorPrimary">@color/bluecolorPrimary</item>
        <item name="colorPrimaryDark">@color/bluecolorPrimaryDark</item>
        <item name="colorAccent">@color/bluecolorAccent</item>
        <item name="android:textColor">@color/DarkThemeText</item>
    </style

<resources>

Utils.java

public class Utils
{
    private static int sTheme;
    public final static int THEME_WITHE = 0;
    public final static int THEME_BLACK = 1;
    public final static int  THEME_BLUE = 2;

    public static void changeToTheme(Activity activity, int theme)
    {
        sTheme = theme;
        activity.finish();
        activity.startActivity(new Intent(activity , activity.getClass()));

    }
    public static void onActivityCreateSetTheme(Activity activity)
    {
        switch (sTheme)
        {
            default:
            case THEME_WITHE:
                activity.setTheme(R.style.whiteTheme);

                break;
            case THEME_BLACK:
                activity.setTheme(R.style.DarkTheme);
                break;
            case THEME_BLUE:
                activity.setTheme(R.style.blueTheme);
                break;
        }
    }

}

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


  private RecyclerView recyclerView;
  List<Note> arrayList = new ArrayList<>();
  private AdapterNote adapter;
  private Toolbar toolbar;

  AlertDialog.Builder alertBuilder;

  FloatingActionButton fab;

  @Override
    protected void onCreate(Bundle savedInstanceState) {



      super.onCreate(savedInstanceState);

    SharedPreferences mSetting = this.getSharedPreferences("Themes",0);
    setTheme(mSetting.getInt("Themes",0));



      setContentView(R.layout.activity_main);


      fab = findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Intent intent = new Intent(MainActivity.this,NoteActivity.class);
              startActivity(intent);
          }
      });



        findViewById(R.id.theme_blue).setOnClickListener(this);
      recyclerView = findViewById(R.id.recyclerView);
      adapter = new AdapterNote(arrayList, MainActivity.this);
      recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
      recyclerView.setAdapter(adapter);
      toolbar = findViewById(R.id.toolBar);
      setSupportActionBar(toolbar);

      alertBuilder = new AlertDialog.Builder(MainActivity.this);


      LoadDataInListview();


  }


  @Override
  protected void onResume() {
    super.onResume();
    LoadDataInListview();

  }

  //sitting
  @Override
  public boolean onOptionsItemSelected(@NonNull MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.setting) {
      Intent activity_setting = new Intent(this, Setting.class);
      startActivity(activity_setting);

    } else if (id == R.id.about) {

    } else if (id == android.R.id.home) {
      onBackPressed();
    }

    return super.onOptionsItemSelected(item);


  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.toolbar_menu, menu);
    return true;
  }


  private void LoadDataInListview() {
//    arrayList = db.getAllData();
    arrayList.clear();
    arrayList = NoteDBProvider.getInstance(this).getAllNotes(null);
    adapter = new AdapterNote(arrayList, MainActivity.this);
    recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
  }


  @Override
  public void onClick(View v) {
    SharedPreferences mSetting =this.getSharedPreferences("Themes",0);
    SharedPreferences.Editor editor = mSetting.edit();



    switch (v.getId())
    {


        case R.id.theme_blue:


                Utils.changeToTheme(this,Utils.THEME_BLACK);
                editor.putInt("Themes",Utils.THEME_BLACK);
                editor.apply();

                break;



    }
  }
}

我认为这个问题是使用方法onResume()的,也许,我删除了此方法,但是没有任何改变,我测试了许多解决此问题的方法,但我做不到。

0 个答案:

没有答案