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

2023-09-07 04:02:10 作者:Unicorn(独角兽)

我想补充两个不同的主题,以我的应用程序(光明与黑​​暗)。用户从菜单中选择它。

I want to add two different themes to my app (light and dark ). User chooses it from menu.

这是我做过什么:

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:

This is the 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:

Styles.xml:

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

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

不过,上运行的应用程序,它始终保持白色的主题,即使菜单中选择了。我在做什么错了?

However, on running the app, it always stays white theme, even after menu select. What am I doing wrong?

感谢您。

推荐答案

您可以做到这一点使用preference活动,让用户选择的主题。结果在你pereference XML创建一个列表preference

You can do that using a preference activity, to allow users to chose the theme. In your pereference xml create a ListPreference

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

这个地方在的setContentView(之前的onCreate()您的活动),或在onResume()

Place this in your activity in onCreate() before setContentView() or in onResume()

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

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

Note: you might need to restart the app for changes to take effect.

这里是数组的一个例子:

And here is an example of the arrays:

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

<string-array name="themesArrayValues">
    <item>"Light"</item>
    <item>"Dark"</item>
</string-array>
 
精彩推荐