在运行时使用文件为Android应用程序设置不同的主题
我想在我的应用程序中添加两个不同的主题(明暗)。用户从菜单中选择它 这就是我所做的:在运行时使用文件为Android应用程序设置不同的主题,android,themes,Android,Themes,我想在我的应用程序中添加两个不同的主题(明暗)。用户从菜单中选择它 这就是我所做的: package com.example; import java.io.*; public class setTheme { void write(int n) throws IOException { File myFile = new File("/mnt/sdcard/theme.txt"); if(!myFile.exists()){
package com.example;
import java.io.*;
public class setTheme {
void write(int n) throws IOException
{
File myFile = new File("/mnt/sdcard/theme.txt");
if(!myFile.exists()){
myFile.createNewFile();
}
if(myFile.exists()){
myFile.delete();
myFile.createNewFile();
}
FileWriter Fr = new FileWriter(myFile);
BufferedWriter Br =new BufferedWriter(Fr);
PrintWriter P =new PrintWriter(Br);
P.println(n);
P.close();
}
static int read() throws IOException
{
int mnum=0;
File myFile = new File("/mnt/sdcard/theme.txt");
if (!myFile.exists()) {
return 1;
}
FileReader fr = new FileReader(myFile);
BufferedReader br = new BufferedReader(fr);
@SuppressWarnings("unused")
String txt = "";
while ((txt = br.readLine()) != null)
{
mnum=Integer.parseInt(br.readLine());
}
br.close();
return mnum;
}
}
这是主要的活动:
package com.example;
import java.io.IOException;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
if (setTheme.read()==1) {
getApplication().setTheme(R.style.LightTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);}
else if(setTheme.read()==2) {
getApplication().setTheme(R.style.DarkTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_help:
Intent intent = new Intent(this, HelpActivity.class);
startActivity(intent);
return true;
case R.id.menu_more:
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://play.google.com/store/apps/developer?id=******"));
startActivity(myWebLink);
return true;
case R.id.menu_lightTheme:
setTheme(1);
Toast.makeText(getApplicationContext(), "Restart app for changes to take place", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_darkTheme:
setTheme(1);
Toast.makeText(getApplicationContext(), "Restart app for changes to take place", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Styles.xml:
<style name="LightTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
</style>
<style name="DarkTheme" parent="@android:style/Theme.Holo"> <style>
然而,在运行应用程序时,它始终保持白色主题,即使在菜单选择之后也是如此。我做错了什么
谢谢。您可以使用“首选项”活动来完成此操作,以允许用户选择主题。
在pereference xml中创建ListPreference
<ListPreference
android:key="THEME_PREF"
android:title="Theme"
android:entries="@array/themesArray"
android:entryValues="@array/themesArrayValues"
android:summary="Enter summary here"
/>
注意:您可能需要重新启动应用程序才能使更改生效
下面是阵列的一个示例:
<string-array name="themesArray">
<item>Light</item>
<item>Dark</item>
</string-array>
<string-array name="themesArrayValues">
<item>"Light"</item>
<item>"Dark"</item>
</string-array>
轻的
黑暗的
“轻”
“黑暗”
关于重启部分,我使用了onRestart(),所以它可以工作!非常感谢。setTheme()作为一个整体不起作用,所以我使用了super.setTheme()。
<string-array name="themesArray">
<item>Light</item>
<item>Dark</item>
</string-array>
<string-array name="themesArrayValues">
<item>"Light"</item>
<item>"Dark"</item>
</string-array>