Android按钮图像使用ActionBar或ActionBarSherlock的主题进行拉伸

txd*_*dev 4 android actionbarsherlock android-actionbar

我刚刚将ActionBar集成到我的Android应用程序中(实际上,我最终使用ActionBarSherlock来实现其向后兼容性).

要在活动上显示ActionBar,您必须使用一组特定的主题:Theme.Holo.*用于ActionBar或Theme.Sherlock.*用于ActionBarSherlock

每当我修改AndroidManifest.xml以使现有活动的样式使用上述ActionBar主题之一时,我发现活动上每个Button的背景图像都会失真.具体而言,图像在比实际包含的图像更多的像素上垂直拉伸.只有当我使用layout_height ="fill_parent"时,我才会期待这种行为,但我不是.此外,其他主题(与ActionBar相关的主题除外)似乎不会发生这种情况.

我试过发布截图,但我还没有足够高的声誉.我道歉.

这是我的一个Button声明受到主题的不利影响:

<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>
Run Code Online (Sandbox Code Playgroud)

由于它引用了一个选择器,我还将包含选择器的XML,其中引用了实际的png文件:

<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>
Run Code Online (Sandbox Code Playgroud)

最后,当发生这种情况时,我正在应用于我的主题的样式:

<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>   
Run Code Online (Sandbox Code Playgroud)

chr*_*lip 7

使用ImageButton并设置android:src ="@ drawable/add_to_bag_selector",不应拉伸你的图像.

你还需要设置android:backgorund ="@ android/color/transparent"