无法从设置更改应用程序主题:(

时间:2012-12-21 06:07:39

标签: java android user-interface themes settings

我在设置主题程序方面遇到了问题,因为在设置中我想要在Holo和Holo Light之间切换时更改优先权,点击设置中的选项。

感谢任何建议,源代码,知识,链接等,谢谢


DashboardActivity.java

public class DashboardActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Utils.onActivityCreateSetTheme(this);
    setContentView(R.layout.dashboard_layout);

    if (Settings.theme.equals("Theme1")) {
        Utils.changeToTheme(this, Utils.THEME_LIGHT);

}else (Settings.theme.equals("Theme2")) {

    Utils.changeToTheme(this, Utils.THEME_DARK);

}

Settings.java

     public class Settings extends PreferenceActivity implements  
          OnSharedPreferenceChangeListener {   

 public static String theme = "Theme1";

    public void onBackPressed() {
    //  ListPreference listPreference2 = (ListPreference) findPreference("activityPref");
        ListPreference listPreference = (ListPreference) findPreference("themePref");
        String currValue = listPreference.getValue();
    //  String currValue2 = listPreference.getValue();
//      Log.d("ssss", "Value :" + currValue);
    //  Log.d("ssss", "Value :" + currValue2);

    //  activity = currValue2;
        theme = currValue;
        super.onBackPressed();

将Settings.xml

   <ListPreference
            android:title="Themes"
            android:summary="Change the UI of the application"
            android:key="themePref"
            android:entries="@array/themesReturnValue"
            android:entryValues="@array/themesDisplayWord" 
            android:defaultValue="Theme1"/>

Array.xml

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

    </string-array>   
    <string-array name="themesDisplayWord">
    <item>Theme1</item>
    <item>Theme2</item>

样式

    <style name="LightThemeAndroid" parent="android:style/Theme.Holo.Light"> 
     </style>
   <style name="DarkThemeAndroid" parent="android:style/Theme.Holo">
    </style>

多数民众赞成所有......我知道这是一项任务,但是我们非常感谢任何帮助。谢谢

1 个答案:

答案 0 :(得分:0)

缺少重要的代码部分,例如Utils.onActivityCreateSetTheme(this);Utils.changeToTheme(this, Utils.THEME_LIGHT);,这可以解释什么是错误/无效。

然而,这就是我为我的应用程序解决它的方式。

的settings.xml

<ListPreference
   android:title="@string/theme_label"
   android:summary="@string/theme_summary"
   android:key="@string/theme_key"
   android:defaultValue="0"
   android:entries="@array/theme_entries"
   android:entryValues="@array/theme_values" />

的strings.xml

<string name="theme_label">Theme</string>
<string name="theme_summary">Change the app theme</string>
<string name="theme_key">theme</string>

arrays.xml

<string-array name="theme_entries" translatable="false">
   <item>Holo Light</item>
   <item>Holo Dark</item>
</string-array>
<string-array name="theme_values" translatable="false">
   <item>0</item>
   <item>1</item>
</string-array>

Util.java

public class Util extends Activity {

public static void setAppTheme(Activity a) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(a);
    int mTheme = Integer.parseInt(sp.getString("theme", "0"));
    if (mTheme == 0) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                a.setTheme(android.R.style.Theme_Holo_Light_DarkActionBar);
                return;
            }
            a.setTheme(android.R.style.Theme_Holo_Light);
        }
        else {
            a.setTheme(android.R.style.Theme_Light);
        }
    }
    else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        a.setTheme(android.R.style.Theme_Holo);
    } else {
        a.setTheme(android.R.style.Theme_Black);
        }
    }
}

MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    Util.setAppTheme(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    }