横向模式下10英寸设备的偏好

时间:2013-08-16 13:37:35

标签: android landscape android-preferences

我无法在面向场景的10英寸设备上正确显示首选项。 它显示类别标题和复选框真的很奇怪。请参阅附图以了解相关信息。 PreferenceScreen

Prefs-XML(稍后只在代码中添加类别,原因字段):

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

<PreferenceCategory
    android:key="settingsCategory"
    android:title="@string/mobile_global_settings" >
</PreferenceCategory>

在SettingsActivity中,我添加PreferenceFragment,如下所示:

protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.Theme_Tp_actionbar_style);
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}

我的PreferenceFragment看起来像这样(我必须为登录到应用程序的不同用户使用不同的settings_keys):

public static class MyPreferenceFragment extends PreferenceFragment
{
    @Override
    public void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);

        TPSettingsActivity act = (TPSettingsActivity)getActivity();
        String userName = "xxx";

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

        PreferenceCategory targetCategory = (PreferenceCategory)findPreference("settingsCategory");

        CheckBoxPreference checkBoxPreference = new CheckBoxPreference(getActivity());
        checkBoxPreference.setKey(userName + "_autoclear");
        checkBoxPreference.setChecked(true);
        checkBoxPreference.setSummary(R.string.mobile_settings_onoff);
        checkBoxPreference.setTitle(R.string.mobile_settings_autoclean);
        checkBoxPreference.setChecked(prefs.getBoolean(userName + "_autoclear", false));
        targetCategory.addPreference(checkBoxPreference);

        TPListPreference listPref = new TPListPreference(getActivity());
        listPref.setKey(userName + "_autoclearperiod");
        listPref.setEntries(R.array.autoclearOptions);
        listPref.setEntryValues(R.array.autoclearValues);
        listPref.setSummary(R.string.mobile_settings_currentinterval);
        listPref.setTitle(R.string.mobile_settings_interval);
        listPref.setValue(prefs.getString(userName + "_autoclearperiod", "7"));
        targetCategory.addPreference(listPref);
    }
}

当不使用App特定主题时也会发生这种情况。我使用的主题是使用Android Asset Studio网站上列出的ThemeGenerator的生成器: Android Asset Studio

1 个答案:

答案 0 :(得分:2)

如果您的应用的API高于11,请执行以下操作:

public class FragmentPreferences extends Activity{
/*..*/
}

而不是:

public class FragmentPreferences extends PreferenceActivity{
    /*..*/
}