2013-03-15 63 views
2

我目前正在嘗試爲偏好設置屏幕設置自定義背景。我創建PreferenceActivity自定義Android偏好背景

public class SettingsActivity extends PreferenceActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    addPreferencesFromResource(R.xml.preferences); 
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false); 
} 
} 

這裏是我的偏好佈局

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
    > 
    <CheckBoxPreference 
     android:key="pref_Autoplay" 
     android:title="@string/pref_Autoplay" 
     android:summary="@string/pref_Autoplay_summ" 
     android:defaultValue="false"/> 
    <ListPreference 
     android:entryValues="@array/pref_Voice_values" 
     android:entries="@array/pref_Voice_entries" 
     android:key="pref_Voice" 
     android:summary="Choose male or female reader" 
     android:title="Readers voice" 
     android:defaultValue="female"/> 


</PreferenceScreen> 

當我讀到此處更改背景我需要創建我自己的主題風格。那就是:

<style name="PrefsTheme" parent="@android:style/Theme.Black.NoTitleBar"> 
     <item name="android:windowBackground">@drawable/prefs_bg</item> 
     <item name="android:textColor">@color/prefs_bg</item> 
     <item name="android:listViewStyle">@style/listViewPrefs</item> 
    </style> 

    <style name="listViewPrefs" parent="@android:style/Widget.ListView"> 
     <item name="android:background">@color/prefs_bg</item> 
     <item name="android:cacheColorHint">@color/prefs_bg</item> 
     <item name="android:textColor">@color/prefs_bg</item> 
    </style> 

所有我需要的是設置首選項列表中的一些圖像背景。我的圖像是@drawable/prefs_bg。當我將windowBackground設置爲圖像並將background設置爲某種顏色時,首選項列表背景將使用顏色而不是圖片。但是當我設置背景@drawable/prefs_bg首選項列表背景與我的圖片和單選按鈕對話框出現我的ListPreference項目也使用圖像背景。

請幫助,我怎樣才能設置圖像背景偏好列表,而不會影響某些偏好項目的單選按鈕對話框的背景?

回答

4

好的。我發現如何解決我的問題。是我的錯。設置優先後臺只需要

<item name="android:windowBackground">@drawable/prefs_bg</item> 

這一個是多餘的:

<item name="android:background">@color/prefs_bg</item> 
<item name="android:cacheColorHint">@color/prefs_bg</item> 
2

要添加動態特性,這也將正常工作。將此添加到PreferenceActivity。我們也可以用同樣的方法設置背景顏色。

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    setupActionBar(); 
    setupSimplePreferencesScreen(); 

    if (Build.VERSION.SDK_INT >= 16) 
     this.getListView().setBackground(getResources().getDrawable(...); 
}