2

I'm trying to have a style with a background image in the top right of the screen. I have this working with

my_theme_background.xml

 <?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
     <item>
         <shape android:shape="rectangle">
        <solid android:color="@color/myThemeBackgroundColor" />
         </shape>
</item>
    <item android:top="70dp">
       <bitmap android:src="@drawable/bg_my_theme"
           android:gravity="top|right" />
    </item>
 </layer-list>

Style

 <style name="MyTheme" parent="AppBaseTheme">
      <item name="android:windowBackground">@drawable/my_theme_background</item>
 </style>

I have to use that android:top="70dp" to keep the image under the action bar. But the action bar changes height on rotation. How do I keep the image under the action bar in the xml without explicitly setting a top value?

codeetcetera
  • 3,213
  • 4
  • 37
  • 62

1 Answers1

0

I believe you can retreive its height via the attribute android.R.attr.actionBarSize. For instance for Honeycomb and later:

android:top="?android:attr/actionBarSize"

Or, according to Jake Wharton, for ActionBarSherlock compatibility:

android:top="?attr/actionBarSize"
Community
  • 1
  • 1
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
  • I'm getting an error with this saying it can't convert it to a dimension. – codeetcetera Apr 08 '13 at 13:46
  • Which SDK level are you building against? Does it compile at all? – Paul Lammertsma Apr 08 '13 at 14:04
  • Minimum 8, Target 17. I'm using actionbar sherlock. The app compiles and runs fine. I explicitly set the actionBarSize in the style and that works fine. I just can't use it as the top value. – codeetcetera Apr 08 '13 at 14:16
  • I gave it a shot, and you're right; I'm getting an `UnsupportedOperationException` that it can't convert it to a dimension. I'm a bit puzzled myself. – Paul Lammertsma Apr 08 '13 at 14:31
  • [This answer appears to suggest](http://stackoverflow.com/a/13026919/154306) that it can only be read at runtime. – Paul Lammertsma Apr 08 '13 at 14:33
  • I know it's not the nice answer you're looking for, but you could alternatively write your own implementation of drawable and resolve the attribute as per the linked answer above. – Paul Lammertsma Apr 08 '13 at 18:45
  • I think the best solution at this point is to explicitly set the height of the actionbar so there won't be a discrepancy between orientations. – codeetcetera Apr 08 '13 at 19:12
  • In this case, I would suggest specifying the dimension through `@dimens/action_bar_height`, and defining it in `values/dimens.xml` and `values-land/dimens.xml`. Personally I would probably write my own drawable. – Paul Lammertsma Apr 08 '13 at 21:05