0

我想为我的应用程序添加两个不同的主题(浅色和深色)。用户从菜单中选择它。

这就是我所做的:

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);
        }
    }

样式.xml:

<style name="LightTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
</style>

<style name="DarkTheme" parent="@android:style/Theme.Holo"> <style>

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

谢谢你。

4

1 回答 1

0

您可以使用偏好活动来做到这一点,以允许用户选择主题。
在您的 preference 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>
于 2013-03-16T08:24:30.887 回答