【问题标题】:Button with 9-patch image background doesn't stretch correctly带有 9-patch 图像背景的按钮无法正确拉伸
【发布时间】:2013-08-21 14:40:30
【问题描述】:

我对按钮的自定义背景有疑问。这是我的 9 补丁:

在 Draw 9-patch 中相同:

最后,结果显示在设备的屏幕上:

这就是描述我的按钮的代码:

布局:

<Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="New Button"
        style="@style/button"/>

风格:

<style name="button">
    <item name="android:background">@drawable/button</item>
    <item name="android:textColor">@color/button_text</item>
</style>

@drawable/button:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" >
        <bitmap android:src="@drawable/button_test"/>
    </item>
</selector>

res文件夹中drawable的文件名是button_test.9.png

您知道为什么它不起作用吗?谢谢。

【问题讨论】:

  • 可能是编译问题。清理并重建?
  • 另外,你应该把drawable直接放在item中:&lt;item android:state_pressed="false" android:drawable="@drawable/button_test"/&gt;

标签: android android-view nine-patch android-styles


【解决方案1】:

移除item标签内的位图标签,在item标签内添加android:drawable属性

【讨论】:

    【解决方案2】:

    试试这个 9 补丁图像:

    希望它有效。

    【讨论】:

      【解决方案3】:

      您需要在图像周围添加至少一个像素厚度的透明周边。 示例图片见官方documentation

      试试这张图片。 (确保使用 .9.png 扩展名保存)

      【讨论】:

      • 在不相关的注释上,请不要approve suggested edits 对非代码使用反引号,而是拒绝或改进它们 - 参见例如here为什么
      猜你喜欢
      • 2012-04-15
      • 1970-01-01
      • 2012-01-30
      • 2019-11-25
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 1970-01-01
      相关资源
      最近更新 更多