【问题标题】:Android button image gets stretched using themes for ActionBar or ActionBarSherlock使用 ActionBar 或 ActionBarSherlock 的主题拉伸 Android 按钮图像
【发布时间】:2012-09-17 20:32:13
【问题描述】:

我刚刚将 ActionBar 集成到我的 Android 应用程序中(实际上,我最终选择了 ActionBarSherlock 是因为它的向后兼容性)。

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

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

我尝试发布屏幕截图,但我还没有足够高的声誉。我很抱歉。

这是我的一个按钮声明,它受到主题的不利影响:

<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>

由于引用了一个选择器,我将包括选择器的 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>

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

<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>   

【问题讨论】:

    标签: android android-actionbar actionbarsherlock


    【解决方案1】:

    使用 ImageButton 并设置 android:src="@drawable/add_to_bag_selector" 并且您的图像不应被拉伸。

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

    【讨论】:

    • MrFox- 你完全正确。感谢您提供极其快速和有益的回复!
    • 如果我的按钮需要有文本和自定义图像怎么办?
    猜你喜欢
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 2011-05-20
    • 1970-01-01
    相关资源
    最近更新 更多