使用文件在运行时为Android应用设置不同的主题

时间:2013-03-16 08:18:23

标签: 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()){
            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;



    }

}

这是mainActivity:

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>

但是,在运行应用程序时,即使菜单选择后,它也始终保持白色主题。我做错了什么?

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用偏好活动来执行此操作,以允许用户选择主题 在你的pereference xml中创建一个ListPreference

<ListPreference 
    android:key="THEME_PREF"
    android:title="Theme"
    android:entries="@array/themesArray"  
    android:entryValues="@array/themesArrayValues"
    android:summary="Enter summary here"
/> 

在setContentView()之前或onResume()

中将它放在onCreate()中的活动中
SharedPreferences mysettings2 = PreferenceManager.getDefaultSharedPreferences(this);
String st1 = mysettings2.getString("THEME_PREF", "Light");
if(st1.equals("Light"))
  {setTheme(R.style....); }
if(st1.equals("Dark"))
  {setTheme(R.style....); }

注意:您可能需要重新启动应用才能使更改生效。

这是一个数组的例子:

<string-array name="themesArray">
    <item>Light</item>
    <item>Dark</item>
</string-array>    

<string-array name="themesArrayValues">
    <item>"Light"</item>
    <item>"Dark"</item>
</string-array>