简体   繁体   English

在Android中使用主题

[英]Using Themes in Android

Im trying to switch between Holo.Light and Holo (Dark) so everything in my application is changed to the theme the user sets in the preferences. 我试图在Holo.Light和Holo(Dark)之间切换,因此应用程序中的所有内容都更改为用户在首选项中设置的主题。

I've been looking at a few open source apps that this is done in and cant seem to make it work with my project, Any help on this would be greately appreciated. 我一直在研究一些开源应用程序,这些应用程序已经完成并且似乎无法使其与我的项目一起使用,对此的任何帮助将不胜感激。

The Current ISSUE im running into inside DashboardActivity.java is: 在DashboardActivity.java内部运行的当前问题IM是:

"I've having issues with "setTheme(Integer.parseInt( pref.getString(" “我在“ setTheme(Integer.parseInt(pref.getString(”

DashboardActivity (Updated) DashboardActivity(已更新)

public class DashboardActivity extends Activity {

public static final int THEME_BLACK = R.style.DarkThemeAndroid;
public static final int THEME_WHITE = R.style.LightThemeAndroid;
public static final int THEME_WHITE_BLACK = android.R.style.Theme_Holo_Light_DarkActionBar;

@Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            // I've having issue with the "pref.getString" i've tried "Settings.getString" and get error about needing a getString method is needed in Settings.java
    setTheme(Integer.parseInt( pref.getString("selectedTheme", String.valueOf(R.style.LightThemeAndroid) )));
    setContentView(R.layout.dashboard_layout);
          // the rest of my code
      )
    )

Settings (Updated) 设置更新)

public class Settings extends PreferenceActivity implements
    OnSharedPreferenceChangeListener {

final static String[] mThemeEntries = {
    "Default (Light)", 
    "Dark"
    };
final static String[] mThemeValues = {  
    String.valueOf(R.style.LightThemeAndroid), 
    String.valueOf(R.style.DarkThemeAndroid)
    };
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Loads the XML preferences file.
    addPreferencesFromResource(R.xml.settings);
//  SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);


    ListPreference listPref = (ListPreference)findPreference("selectedTheme");

    listPref.setEntries(mThemeEntries);
    listPref.setEntryValues(mThemeValues);

    listPref.setValue( pref.getString("selectedTheme", String.valueOf(mThemeValues[0]) ) );

XML Resources XML资源

Themes.xml Themes.xml

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

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

Settings.xml Settings.xml

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

Arrays.xml Arrays.xml

 <string-array name="themesReturnValue">
   <item>Light</item>
    <item>Dark</item>
    <item>LightActionBar</item>
         </string-array>   
<string-array name="themesDisplayWord">
    <item>Theme1</item>
    <item>Theme2</item>
    <item>Theme3</item>
        </string-array>

getTheme() is a non-static method and you are trying to call it in a static way (not from an instance variable). getTheme()是一个非静态方法,您正在尝试以静态方式(而不是从实例变量)调用它。 You need an instance of the object to call the non-static method. 您需要对象的实例才能调用非静态方法。 Also, the docs state 此外,文档状态

Note that this should be called before any views are instantiated in the Context (for example before calling setContentView(View) or inflate(int, ViewGroup)). 请注意,应在Context中实例化任何视图之前(例如,在调用setContentView(View)或inflate(int,ViewGroup)之前)调用此方法。

Docs 文件

There exists non-static getTheme() method of the ContextThemeWrapper class that returns Theme object type. 存在ContextThemeWrapper类的非静态getTheme()方法,该方法返回主题对象类型。 Change the name of the getTheme() method to a different one for example getThe() and you should be fine. 将getTheme()方法的名称更改为其他名称,例如getThe(),您应该会很好。

First like codeMagic said, you are calling getTheme in a non-static way, I would also put the theme related constants in 1 class. 首先,就像codeMagic所说的那样,您以一种非静态的方式调用getTheme,我还将把与主题相关的常量放在1类中。 Try making getTheme a static method (Although i would probably create a seperate class just for theme). 尝试使getTheme成为静态方法(尽管我可能会为主题创建一个单独的类)。 Apart from that, here is a solution I was working on 2 days ago as i couldn't find anywhere on google or here for an answer to a more dynamic fluid theme preference. 除此之外,这是我两天前正在研究的解决方案,因为我在google或此处找不到任何地方,无法找到更加动态的主题偏好的答案。

I start off with my preference fragment (in your case, activity). 我从我的偏好片段(在您的情况下是活动)开始。 Then declare the themes and values into an array... 然后将主题和值声明到数组中...

public class LayoutFragment extends PreferenceFragment {

final static String[] mThemeEntries = {
    "Default (Light)", 
    "Dark"
    };
final static String[] mThemeValues = {  
    String.valueOf(R.style.Theme_Default), 
    String.valueOf(R.style.Theme_Dark)
    };

then in my oncreate method, i set these values (note, in the xml i do not set these values and there are no references to those values in the xml files) 然后在我的oncreate方法中,设置这些值(注意,在xml中,我没有设置这些值,并且在xml文件中没有对这些值的引用)

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);                 
    addPreferencesFromResource(R.xml.pref_layout);
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity());

    ListPreference listPref = (ListPreference)findPreference("selectedTheme");

    listPref.setEntries(mThemeEntries);
    listPref.setEntryValues(mThemeValues);

    listPref.setValue( pref.getString("selectedTheme", String.valueOf(mThemeValues[0]) ) );

}

Notice that i had converted these values to a string, dont ask why, but i was getting a whole lot of trouble trying to store the int value of those resource id's (this is also why xml values was not an option)... 请注意,我已经将这些值转换为字符串,不要问为什么,但是尝试存储那些资源ID的int值时遇到了很多麻烦(这也是为什么xml值不是选项的原因)...

now when ever i need to set a theme from the user preference... I just call 现在,当我需要根据用户偏好设置主题时,...

setTheme(Integer.parseInt( pref.getString("selectedTheme", String.valueOf(R.style.Theme_Default) )));

and off course you could provide a static method somewhere to do the castings etc without writing it for each activity. 当然,您可以在某处提供静态方法来进行转换等,而无需为每个活动编写该方法。

EDIT: You don't need the array's to be static, I must off left them like that while I was trying different things to get it to work. 编辑:您不需要数组是静态的,我在尝试其他方法使其工作时必须像这样离开它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM