【问题标题】:How to set own background in style of button in Android如何在Android中以按钮样式设置自己的背景
【发布时间】:2015-02-08 18:24:57
【问题描述】:

我在样式文件中制作了自己的按钮样式。它看起来像这样:

按钮样式

  <style name="ButtonSettings" parent="@android:style/Widget.Button">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
    <item name="android:background">@drawable/BaseButtonDesign</item>
 </style>

BaseButtonDesign.xml

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/BaseButtonPressed"
        android:state_pressed="true" />
  <item android:drawable="@drawable/BaseButtonPressed"
        android:state_focused="true" />
  <item android:drawable="@drawable/BaseButtonReleased" />
</selector>

但是当我编译这段代码时,编译器给了我错误消息 - 找不到与给定名称匹配的资源(在 'android:background' 处,值为 '@drawable/BaseButtonDesign')

【问题讨论】:

  • 确保您的代码已编译。
  • 您将文件放在哪个文件夹中?

标签: android xml button styles


【解决方案1】:

我建议您只使用小写字母、数字和下划线作为可绘制名称的名称

【讨论】:

    【解决方案2】:

    你的文件名对吗? drawable 中的文件名只能包含小写字母和数字([a-z0-9_.])。

    【讨论】:

    • 谢谢。 CamelCase 命名适用于布局设计,但不适用于组件样式。你的小费解决了我的问题;-)
    猜你喜欢
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多