Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 应用程序主题未随选择的首选项而更改?_Android - Fatal编程技术网

Android 应用程序主题未随选择的首选项而更改?

Android 应用程序主题未随选择的首选项而更改?,android,Android,这是我第一次尝试使用首选项来更改应用程序主题,但我似乎无法正常工作。这是我的密码: Java onCreate方法 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseConte

这是我第一次尝试使用首选项来更改应用程序主题,但我似乎无法正常工作。这是我的密码:

Java onCreate方法

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        String values = getPrefs.getString("list","1");
        if(values.contentEquals("1")){
            setTheme(R.style.Holo_Dark);

        }else if(values.contentEquals("2")){
            setTheme(R.style.AppTheme);

        }

        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

        TextView medEntry = (TextView)findViewById(R.id.med_name);

       /* medEntry.setOnFocusListener(new FocusListner(){

        }); */

    }
Prefs.Java:

public class Prefs extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState){



        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);

    }
}
prefs的array.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="list">

        <item>Dark</item>
        <item>Light</item>
    </string-array>
    <string-array name="lValues">
        <item>1</item>
        <item>2</item>
    </string-array>
</resources>

黑暗的
轻的
1.
2.
styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- Customize your theme here. -->

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

    </style>

</resources>

还有我的舱单:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Prefs"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.PREFS" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

使用此代码

if(values.equals("1") 
而不是

if(values.contentEquals("1"))

提供一个字符串,或者设置一个常量,以最大限度地减少拼写错误的风险。
如果你在prefs中搞砸了,别忘了卸载并重新安装应用程序

这并没有改变结果Post您将值提交到编辑器的代码可能在整个应用程序的列表字符串中只有一个值,这就是为什么您会看到相同的值。如上所示,这些值在array.xml中,如上所示,这些值可以在prefs.java中提交,如上所示@ravi sharma