13

I have a set of similar applications. I extracted common resources and code to the library project and applications just override what they need to. In my library there is the following style defined:

<style name="ListItemText">
    <item name="android:layout_toRightOf">@id/preview_image</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">@dimen/previewTextSize</item>
</style>

As you can see it contains android:layout_toRightOf attribute as this style would be applied to the text in the ListView row which should be displayed to the right of the image in that row.
However in one of my applications I'd like to display the text below the image. How the ListItemText style in that application should be defined to override android:layout_toRightOf attribute value and replace it with android:layout_below?

If I define it as:

<style name="ListItemText">
    <item name="android:layout_below">@id/preview_image</item>
</style>

it displays text to the right and below the image, effectively summing up attributes from both library and application styles XMLs.

p.s.: One possible solution of my issue would be to get rid of android:layout_toRightOf in the styles and move it to the layout xml instead. Then in the target application this layout can be redefined/overridden. But I'm looking for style-based solution, since it could provide more simple and straightforward way of attribute overriding.
(I can also use the inherited style with the parent attribute, but this would again require layout overriding in the application, which I try to avoid).

Volo
  • 28,673
  • 12
  • 97
  • 125

2 Answers2

11

To disable android:layout_toRightOf, you can set it to @null. If your app defines the ListItemText style as follows, it should work as you want:

<style name="ListItemText">
    <item name="android:layout_toRightOf">@null</item>
    <item name="android:layout_below">@id/preview_image</item>
</style>
Martin Nordholts
  • 10,338
  • 2
  • 39
  • 43
  • Works as intended, thanks. Is this `@null` reference specified somewhere in the documentation? – Volo Oct 25 '11 at 10:01
  • 1
    Not in any obvious place as far as I know, but it is mentioned, like here for example: http://developer.android.com/resources/articles/window-bg-speed.html – Martin Nordholts Oct 25 '11 at 11:34
0

You could use "parent" to derive a specialized style (see http://developer.android.com/guide/topics/ui/themes.html) but defining the layouts like this with styles doesn't strike me as the way to carve this problem up. It's ok to leave some layout info in the layout files.

Edwin Evans
  • 2,726
  • 5
  • 34
  • 47
  • Yes, I know about "parent". But it won't help in my case since the layout is defined inside the library and it references `ListItemText` style. I could redefine style in the application of course (see my p.s.), but would like to know whether style-based solution is possible. – Volo Oct 15 '11 at 19:15