4

I just integrated ActionBar into my Android app (actually, I ultimately went with ActionBarSherlock for its backward compatibility).

To display the ActionBar on an activity, you must use a particular set of themes: Theme.Holo.* for ActionBar or Theme.Sherlock.* for ActionBarSherlock

Whenever I modify the AndroidManifest.xml so that the style of an existing activity uses one of the aforementioned ActionBar themes, I find that the background image of each Button on the activity becomes distorted. Specifically, the image gets stretched vertically across more pixels than the image actually contains. I would expect that behavior only if I was using layout_height="fill_parent", but I am not. Furthermore, this doesn't seem to happen with other themes (other than the ActionBar-related themes).

I tried posting screenshots, but I don't have a high enough reputation yet. I apologize.

Here is one of my Button declarations that is being affected adversely by the Theme:

<Button
    android:textStyle="bold"
    android:typeface="sans"
    android:layout_height="wrap_content"
    android:id="@+id/btnAddtoBag" android:layout_width="wrap_content"
    android:background="@drawable/add_to_bag_selector">
</Button>

Since that references a selector, I will include the XML of the selector also, where the actual png files are referenced:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/selected_add_to_bag" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/selected_add_to_bag" /> <!-- focussed -->
    <item android:drawable="@drawable/default_add_to_bag" /> <!-- default -->
</selector>

Finally, here is the style that I'm applying to my theme when this happens:

<style name="Theme.CustomActionBar" parent="Theme.Sherlock.Light.DarkActionBar.ForceOverflow">
    <item name="actionBarStyle">@style/ActionBar</item>
</style>

<style name="ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
    <item name="android:displayOptions">showHome|useLogo</item>
    <item name="displayOptions">showHome|useLogo</item>     
</style>   
txdev
  • 257
  • 2
  • 13

1 Answers1

7

use ImageButton and set the android:src="@drawable/add_to_bag_selector" and your image shouldn't be stretched.

you will also need to set the android:backgorund="@android/color/transparent"

chris-tulip
  • 1,840
  • 1
  • 15
  • 22