2012-08-01 109 views
11

我想改变我的应用程序PreferenceActivity的主题,我只是不能得到它的工作。如何更改PreferenceActivity主题?

这是XML:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 

     <SwitchPreference android:title="Auto Clear" android:key="autoclear" android:summary="Clear the command line when the code is being executed." android:defaultValue="false"/> 
     <ListPreference android:title="Choose a theme" android:negativeButtonText="" android:dialogTitle="" android:key="theme" android:entries="@array/themesList" android:entryValues="@array/themesList" android:defaultValue="Default" /> 

</PreferenceScreen> 

这是PreferenceActivity:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    this.setTheme(R.style.AppTheme); 

    addPreferencesFromResource(R.xml.preferences); 

} 

,其结果是:

Result

+0

你想改变整个应用程序的主题还是只是PreferenceActivity? – 2012-08-01 02:20:05

+0

你见过这些。 http://udinic.wordpress.com/2011/08/18/dress-up-your-preferenceactivity/ http://liquidlabs.ca/2011/10/17/override-android-preference-activity-colors/ – san 2012-08-01 03:30:04

回答

16

您是否尝试过在应用主题清单中的活动标签?这是我如何做之前 -

<activity 
    android:label="@string/app_name" 
    android:name="com.example.MyPreferenceActivity" 
    android:theme="@android:style/Theme.Black" 
    android:exported="true" 
    android:icon="@drawable/ic_launcher"></activity> 

编辑:

你可以尝试另一种选择是重写onApplyThemeResource(Resources.Theme theme, int resid, boolean first)。查看android源代码setTheme将在内部调用方法。

/** 
* Called by {@link #setTheme} and {@link #getTheme} to apply a theme 
* resource to the current Theme object. Can override to change the 
* default (simple) behavior. This method will not be called in multiple 
* threads simultaneously. 
* 
* @param theme The Theme object being modified. 
* @param resid The theme style resource being applied to <var>theme</var>. 
* @param first Set to true if this is the first time a style is being 
*    applied to <var>theme</var>. 
*/ 
protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean first) { 
    theme.applyStyle(resid, true); 
} 
+0

但我想以编程方式更改主题,因为在设置中用户可以更改主题。所以每次PreferenceActivity启动时,我都想通过代码更改主题。 – 2012-08-01 02:15:27

+0

啊,我明白了。我已经更新了我的帖子。希望有助于解决问题。 – shri046 2012-08-01 02:41:05

+0

Thanx男人!有效!!! – 2012-08-01 09:54:39

4

如果你想改变的背景,你可以使用

public class FractalPreferenceActivity extends PreferenceActivity { 
    ....... 

    @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getWindow().setBackgroundDrawableResource(R.drawable.gradient); 
    getListView().setBackgroundColor(Color.TRANSPARENT); 
    getListView().setCacheColorHint(Color.TRANSPARENT); 

      ....... 
    } 

}

+0

这不仅仅是背景,R.style.AppTheme是一个全新的主题。 – 2012-08-01 09:48:52

8

最后,我发现了如何改变 “PreferenceActivity” 主题编程方式(通过java代码)

要改变主题就像这样:

 @Override 
     public void onCreate(Bundle savedInstanceState) { 
     setTheme(R.style.Holo_Theme_Light); 
     super.onCreate(savedInstanceState); 
     } 

总是在super.onCreate(savedInstanceState);方法之前调用setTheme(R.style.yourtheme);方法。通过这样做会产生如下所示的结果。

enter image description here

这就是全部。

如果您通过super.onCreate(savedInstanceState);方法调用setTheme(R.style.yourtheme);方法,它将产生如下所示的结果。

enter image description here

注:主题不被嵌套PreferenceScreen认识。要将主题应用于该嵌套的PreferenceScreen,您必须为该嵌套的PreferenceScreen制作另一个PreferenceActivity,并在该处调用setTheme(R.style.yourtheme);方法。

0

我个人使用这种方法: 的API低于11我用(价值目录,文件的themes.xml):

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <style name="MyTheme" parent="@android:style/Theme.Light.NoTitleBar"></style> 

</resources> 

更高(例如值-V14目录,主题。xml文件):

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <style name="MyTheme" parent="@android:style/Theme.Holo.Light.NoActionBar"></style> 

</resources> 

和清单:

<application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

Android将选择基于设备的API自动为主题,并没有必要的活动(清单)或语法的代码指定任何主题.. 。

themes

0

这会听起来很蠢,但使用setTheme()将工作在所有的碎片,但不是主要的prefe rence活动。在清单文件中设置它,它会突然开始工作。

我动态设置主题,根据用户的喜好,从主题管理器对象调用setTheme()onCreate之前(也多尔斯的产品,可以说我不想在风格设定主题改变各种颜色和drawables。主要是因为它弄不清楚是什么)。

不知道为什么设置它在清单使setTheme()工作。只是一些故障特定于偏好活动,我猜。