SoFunction
Updated on 2025-04-09

Example of how to dynamically modify topics for Android applications

1. Set up topics using API

As shown below, use setTheme in Activity

setTheme(.MyTheme1);

2. The timing of calling the API

Need to be in(savedInstanceState)Called setTheme before

3. Reset the theme

To reset the topic, you need to end the activity and restart it

public class MainActivity extends Activity {
  private TextView tv;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    if(MyApplication.SHOW_THEME_FLAG == MyApplication.SHOW_1_THEME_FLAG){
      setTheme(.MyTheme2);
    }else if(MyApplication.SHOW_THEME_FLAG == MyApplication.SHOW_2_THEME_FLAG){
      setTheme(.MyTheme1);
    }
    (savedInstanceState);
    setContentView(.activity_main);
    tv = findViewById();
    getActionBar().show();
    getActionBar().setTitle("ActionBar Title");
    (new () {
      @Override
      public void onClick(View view) {
        if(MyApplication.SHOW_THEME_FLAG == MyApplication.SHOW_1_THEME_FLAG){
          MyApplication.SHOW_THEME_FLAG = MyApplication.SHOW_2_THEME_FLAG;
        }else if(MyApplication.SHOW_THEME_FLAG == MyApplication.SHOW_2_THEME_FLAG){
          MyApplication.SHOW_THEME_FLAG = MyApplication.SHOW_1_THEME_FLAG;
        }
        Intent mIntent = getIntent();
        finish();
        overridePendingTransition(0,0);
        startActivity(mIntent);
      }
    });
  }
}

4. Common theme style settings

  <style name="MyTheme1" parent="@android:style/">
    <!-- Status bar -->
    <item name="android:colorPrimaryDark">#f00</item>
    <!-- ActionBarcolor -->
    <item name="android:colorPrimary">#ff0</item>
    <!-- Interface background color -->
    <item name="android:windowBackground">@color/windowBackgroundColor1</item>
    <!-- 导航栏color -->
    <item name="android:navigationBarColor">#0000ff</item>
  </style>
  <style name="MyTheme2" parent="@android:style/">
    <!-- Status bar -->
    <item name="android:colorPrimaryDark">#757575</item>
    <!-- ActionBarcolor -->
    <item name="android:colorPrimary">#03A9F4</item>
    <!-- Interface background color -->
    <item name="android:windowBackground">@color/windowBackgroundColor2</item>
    <!-- 导航栏color -->
    <item name="android:navigationBarColor">#1976D2</item>
  </style>

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the relevant links below