Android Preferences - changing the order in which

2019-05-26 10:35发布

问题:

I'm trying to create a hierarchy of classes which start with a PreferenceActivity.

The idea is the base class contains the 'About Me' button, then the next class implements the generic preferences for games/apps/wallpapers and then the next class adds the the specifics for each actual Game/App.

The idea works well BUT because I'm 'adding' preferences from XML at each level, they appear with the most generic (lowest level) ones first (e.g. my 'About' button).

Ideally I'd like them to be the other way up - so that specific preferences appear at the top of the list and generic ones at the bottom - is there any way of achieving this (I've read through the docs on PreferenceFragments and the new PreferenceHeaders and even that doesn't seem to offer anything?)

Is there any programmatic way to move PreferenceCategorys or PreferenceScreens around within the overall order??

Essentially when I'm using

addPreferencesFromResource(R.xml.clock_settings);

I'd like to have a

addPreferencesFromResourceAtBottom(R.xml.clock_settings);

回答1:

I'm getting good at answering my own questions here :)

The solution lies in the android:order XML parameter - using that with Preference (or PreferenceGroup or a sub-class of it) will sort stuff into the right sequence on screen!

You could also change this dynamically with setOrder() but it won't reorder the Preferences if they've already been added to a PreferenceGroup.