我正在尝试动态更改应用程序的主题。但我只能在应用程序运行时更改它。意思是,当我终止应用程序并再次打开它时,将应用默认主题。
MainActivity.java
public class MainActivity extends AppCompatActivity {
public ArrayList<FTPFile> listfile=null;
Intent men;
@Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences preferences=getSharedPreferences("ThemePreferences",MODE_PRIVATE);
String choice=preferences.getString("CurrentTheme",null);
Toast.makeText(getApplicationContext(),"Its"+ choice,Toast.LENGTH_SHORT).show();
if(choice=="Light")
this.setTheme(R.style.AppThemeLight);
else
this.setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity" android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".file_explorer" android:theme="@style/AppTheme" />
<activity android:name=".settings" android:theme="@style/AppTheme" />
</application>
将值更改为共享首选项的代码
SharedPreferences preferences=getSharedPreferences("ThemePreferences",MODE_PRIVATE);
Editor editor=preferences.edit();
RadioGroup rg=findViewById(R.id.radgrp);
犯罪嫌疑人X
慕村9548890
慕森王
相关分类