问题
I've created my own custom preference objects that extend Preference. I've only created them because there are no Preferences for these custom data types.
Everything is working, but my custom preferences don't have the same appearance because they are missing the horizontal divider that the system preference objects have. I've looked for the code that is creating the horizontal divider, but I can't find where it is done.
The built in divider isn't just a thin bar. There is a bit of a gradient to it. I'm thinking that this may be in a layout file, but I can't find the layouts for the system preferences.
Anybody got an idea how this is implemented?
回答1:
Very old post, but to those who stumble upon this. Wasn't sure if the OP was asking how to change the divider or where the divider images come from. So I'll address both.
HowPreferences
uses a ListView
for populating all the individual preferences. That means you can change the divider by using the .setDivider()
method from ListView
. A PreferenceActivity
will already have the getListView()
method for you. However for PreferenceFragments
just use the android.R.id.list
ID to find it.
If you don't want to change the divider through code, you can always use a theme by overriding the listDivider
attribute. Eg:
<item name="android:listDivider">@drawable/custom_divider</item>
Note, that will change the divider for EVERY ListView
in your app.
Where
The listDivider drawable used depends on what Android theme is activated. You'll find all these images in the installed Android SDK at this location:
[Android SDK]/platforms/[API]/data/res/drawable-[DPI]/
Just do a search for `*divider_horizontal*`, and you'll turn up quite a few. They are nine-patched and not all of them are solid colors.
来源:https://stackoverflow.com/questions/5481896/android-preference-horizontal-divider-in-custom-preference