我将PreferenceScreen用于我的设置。
该活动适用于 @android:style / Theme.Dialog 主题。 因此,对话框出现在我的主要活动中心!
它在ldpi,mdpi和hdpi上工作正常但在xhdpi上不起作用。 切换首选项覆盖了我的标题项!创建边距= s
好(ldpi,mdpi,hdpi):http://img405.imageshack.us/img405/9212/sty.png
差(xhdpi):http://img560.imageshack.us/img560/3529/c7k.png
如何删除此内容? 谢谢!
XML:
<?xml version="1.0" encoding="utf-8"?>
<!-- Réglages ! -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Capture categorie ! -->
<PreferenceCategory
android:title="@string/settings_label_capture"
android:key="pref_key_capture_settings" >
<!-- Vitesse -->
<ListPreference
android:key="speed_preferences"
android:title="@string/settings_speed"
android:summary="@string/settings_speed_summary"
android:entries="@array/speedUnity"
android:entryValues="@array/speedValues" />
<!-- Mode -->
<ListPreference
android:key="mode_preferences"
android:title="@string/settings_mode"
android:summary="@string/settings_mode_summary"
android:entries="@array/mode"
android:entryValues="@array/modeValues" />
<!-- Historique -->
<SwitchPreference
android:key="lock_preferences"
android:title="@string/settings_historic"
android:summary="@string/settings_historic_summary"
android:switchTextOn="@string/settings_historic_lock"
android:switchTextOff="@string/settings_historic_unlock"/>
</PreferenceCategory>
<!-- Stockage -->
<PreferenceCategory
android:title="@string/settings_storage"
android:key="pref_key_storage">
<!-- Sauvegarde -->
<SwitchPreference
android:key="save_preferences"
android:title="@string/settings_save"
android:summary="@string/settings_save_summary"
android:switchTextOff="@string/settings_save_manual"
android:switchTextOn="@string/settings_save_auto"/>
</PreferenceCategory>
代码:
public class Settings extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener{
protected Method mLoadHeaders = null;
protected Method mHasHeaders = null;
/**
* Checks to see if using new v11+ way of handling PrefsFragments.
* @return Returns false pre-v11, else checks to see if using headers.
*/
public boolean isNewV11Prefs() {
if (mHasHeaders!=null && mLoadHeaders!=null) {
try {
return (Boolean)mHasHeaders.invoke(this);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
return false;
}
@Override
public void onCreate(Bundle aSavedState) {
//onBuildHeaders() will be called during super.onCreate()
try {
mLoadHeaders = getClass().getMethod("loadHeadersFromResource", int.class, List.class );
mHasHeaders = getClass().getMethod("hasHeaders");
} catch (NoSuchMethodException e) {
}
super.onCreate(aSavedState);
if (!isNewV11Prefs()) {
addPreferencesFromResource(R.xml.settings);
}
SharedPreferences sharedPref = getPreferenceScreen().getSharedPreferences();
ListPreference pref = (ListPreference) findPreference("speed_preferences");
switch (Integer.valueOf(sharedPref.getString("speed_preferences", "0"))) {
case Vitesse.KMH:
pref.setSummary(getResources().getString(R.string.settings_speed_summary) + " : " + getResources().getString(R.string.settings_speed_kmh));
break;
case Vitesse.MS:
pref.setSummary(getResources().getString(R.string.settings_speed_summary) + " : " + getResources().getString(R.string.settings_speed_ms));
break;
case Vitesse.MPH:
pref.setSummary(getResources().getString(R.string.settings_speed_summary) + " : " + getResources().getString(R.string.settings_speed_mph));
break;
}
pref = (ListPreference) findPreference("mode_preferences");
if (sharedPref.getString("mode_preferences", Preferences.Mode.SIMPLE.toString()).equals(Preferences.Mode.SIMPLE.toString())) {
pref.setSummary(getResources().getString(R.string.settings_mode_summary) + " : " + getResources().getString(R.string.settings_mode_single));
} else {
pref.setSummary(getResources().getString(R.string.settings_mode_summary) + " : " + getResources().getString(R.string.settings_mode_burst));
}
}
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
Preference pref;
if ((pref = findPreference(s)) instanceof ListPreference) {
ListPreference listPref = (ListPreference) pref;
if (listPref.getKey().equals("speed_preferences")) {
listPref.setSummary(getResources().getString(R.string.settings_speed_summary) + " : " + listPref.getEntry());
} else if (listPref.getKey().equals("mode_preferences")) {
listPref.setSummary(getResources().getString(R.string.settings_mode_summary) + " : " + listPref.getEntry());
}
}
}
@Override
public void onBuildHeaders(List<Header> aTarget) {
try {
mLoadHeaders.invoke(this,new Object[]{R.xml.settings,aTarget});
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
static public class PrefsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener{
@Override
public void onCreate(Bundle aSavedState) {
super.onCreate(aSavedState);
addPreferencesFromResource(R.xml.settings);
}
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
Preference pref;
if ((pref = findPreference(s)) instanceof ListPreference) {
ListPreference listPref = (ListPreference) pref;
if (listPref.getKey().equals("speed_preferences")) {
listPref.setSummary(getResources().getString(R.string.settings_speed_summary) + " : " + listPref.getEntry());
} else if (listPref.getKey().equals("mode_preferences")) {
listPref.setSummary(getResources().getString(R.string.settings_mode_summary) + " : " + listPref.getEntry());
}
}
}
}
}