[英]How to change PreferenceActivity theme?
我正在嘗試在我的應用中更改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);
}
結果是:
您是否嘗試在清單中的活動標簽上應用主題? 這是我以前做過的 -
<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);
}
最后我發現了如何以編程方式更改主題“PreferenceActivity”(通過java代碼)
要更改主題,請執行以下操作:
@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Holo_Theme_Light);
super.onCreate(savedInstanceState);
}
總是打電話給setTheme(R.style.yourtheme);
super.onCreate(savedInstanceState);
之前的方法super.onCreate(savedInstanceState);
方法。 通過這樣做,它將產生如下所示的結果。
就這樣。
如果喲叫setTheme(R.style.yourtheme);
super.onCreate(savedInstanceState);
之后的方法super.onCreate(savedInstanceState);
方法它將產生如下所示的結果。
注意:嵌套的PreferenceScreen無法識別主題。 要將主題應用於嵌套的PreferenceScreen,您必須為該嵌套的PreferenceScreen制作另一個PreferenceActivity並調用setTheme(R.style.yourtheme);
方法那里。
如果要更改可以使用的背景
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);
.......
}
}
我個人使用這種方法:對於低於11的API,我使用(values directory,themes.xml文件):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="@android:style/Theme.Light.NoTitleBar"></style>
</resources>
for higher(例如values-v14目錄,themes.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的主題,無需在活動(清單)中指定任何主題或在代碼中指定語法...
這聽起來很愚蠢,但使用setTheme()
將對所有碎片起作用,但不會對主要的偏好活動起作用。 在清單文件中設置它,它將突然開始工作。
我根據用戶首選項動態設置主題,在主題管理器對象的onCreate
之前調用setTheme()
(它還會顯示各種顏色和drawables
,我不想在樣式中設置主題...主要是因為它太混亂,無法弄清楚是什么)。
不知道為什么在清單中設置它會使setTheme()
工作。 我認為只是針對偏好活動的一些故障。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.