11

I'm trying to build an Android UI via layouts. I start with the following:

<TextView
    android:id="@+id/..."
    android:layout_marginTop="8dip"
    android:text="..."
    style="?android:attr/listSeparatorTextViewStyle"/>

And that looks good (all caps, smaller font, dividing bar underneath it). Now I want to extend the style, so I change it to the following:

<TextView
    android:id="@+id/..."
    android:layout_marginTop="8dip"
    android:text="..."
    style="@style/section_title"/>

With a style of:

<style name="section_title" parent="@android:attr/listSeparatorTextViewStyle">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
</style>

And that doesn't work (font is correct, but the divider line is gone).

How come... that?

Hounshell
  • 5,321
  • 4
  • 34
  • 51

1 Answers1

27

When you're using:

style="?android:attr/listSeparatorTextViewStyle"

you're using the style pointed by this attribute(listSeparatorTextViewStyle). If you look in the platform themes.xml you'll see that the style that is actually used for this attribute is Widget.TextView.ListSeparator.White. So this is the style you should extend in your custom style.

Unfortunately that style is private and you can't extend it, or you shouldn't extend it(for reference, see this bug report from google). Your best option would be to copy that entire style, Widget.TextView.ListSeparator.White(Widget.TextView.ListSeparator isn't public as well so would have to also copy that), in your custom style and use that instead of extending the style from the android platform(see this response from the link above).

user
  • 86,916
  • 18
  • 197
  • 190
  • 1
    That makes total sense now. This seems like an ugly solution though. To match the system style I'll have to have a (potentially) different style for each version of Android, and if a manufacturer changed this style in the name of skinning, I'm out of luck and just won't be able to match the system style for that device. – Hounshell Jun 03 '12 at 16:22
  • 1
    @Hounshell You could of use a custom theme and set your own style for the `listSeparatorTextViewStyle` attribute. With this, your own style will be present on every android platform. Of course this will still make you vulnerable to the phone manufacturer changing some styles(the only real problem is the background drawable attribute). You don't say **how** exactly does your layout uses this style, but keep in mind that users probably aspect some UI differences in an app. – user Jun 03 '12 at 16:43
  • @GopalSinghSirvi You go and have a look through the files themes.xml and styles.xml in the sdk. – user Aug 28 '15 at 06:23
  • In fact, the style pointed to by this attribute is theme-dependent. `Widget.TextView.ListSeparator.White` is what the `Theme.Holo.Light` theme maps it to, other themes will differ. – user149408 Apr 13 '16 at 10:52