2012-08-01 107 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()工作。只是一些故障特定於偏好活動,我猜。