0

我更改了按钮的背景颜色,现在不再有点击效果。

为了恢复效果,我的方法是为按钮提供不同的背景颜色。

我试过这样:

在 drawable-mdpi 中,我创建了一个button_bg.xml.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

然后,在strings.xml我添加

<style name="darkbutton" parent="@android:style/Theme.Black">
    <item name="android:background">@drawable/button_bg.xml</item>
    <item name="android:textColor">#000000</item>
</style>

问题是,当我尝试运行我的应用程序时出现以下错误:

05-06 12:56:43.796: E/AndroidRuntime(20672): FATAL EXCEPTION: main
05-06 12:56:43.796: E/AndroidRuntime(20672): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.unserekinder/com.example.unserekinder.NeuesKind}: android.view.InflateException: Binary XML file line #24: Error inflating class <unknown>
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.os.Looper.loop(Looper.java:137)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.app.ActivityThread.main(ActivityThread.java:5041)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at java.lang.reflect.Method.invokeNative(Native Method)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at java.lang.reflect.Method.invoke(Method.java:511)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at dalvik.system.NativeStart.main(Native Method)
05-06 12:56:43.796: E/AndroidRuntime(20672): Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class <unknown>
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.app.Activity.setContentView(Activity.java:1881)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at com.example.unserekinder.NeuesKind.onCreate(NeuesKind.java:22)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.app.Activity.performCreate(Activity.java:5104)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-06 12:56:43.796: E/AndroidRuntime(20672):    ... 11 more
05-06 12:56:43.796: E/AndroidRuntime(20672): Caused by: java.lang.reflect.InvocationTargetException
05-06 12:56:43.796: E/AndroidRuntime(20672):    at java.lang.reflect.Constructor.constructNative(Native Method)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
05-06 12:56:43.796: E/AndroidRuntime(20672):    ... 24 more
05-06 12:56:43.796: E/AndroidRuntime(20672): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-xhdpi/button_bg.xml from drawable resource ID #0x7f020000
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.content.res.Resources.loadDrawable(Resources.java:1953)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.view.View.<init>(View.java:3330)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.widget.TextView.<init>(TextView.java:583)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.widget.Button.<init>(Button.java:107)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.widget.Button.<init>(Button.java:103)
05-06 12:56:43.796: E/AndroidRuntime(20672):    ... 27 more
05-06 12:56:43.796: E/AndroidRuntime(20672): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:178)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:885)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822)
05-06 12:56:43.796: E/AndroidRuntime(20672):    at android.content.res.Resources.loadDrawable(Resources.java:1950)
05-06 12:56:43.796: E/AndroidRuntime(20672):    ... 32 more

我的问题可能是什么?

4

2 回答 2

0

我认为您必须从 string.xml 中的@drawable/button_bg.xml中删除 .xml 扩展名

于 2013-05-06T10:00:43.950 回答
0

正如错误所说,您需要将android:drawable属性包含<item/>button_bg.xml. 改变你button_bg.xml的东西是这样的:

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

colors.xml在您的 res/values/ 文件夹中创建一个以保存所有颜色值:

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
      <color name="pressed">#ffff0000</color>
      <color name="focused">#ff0000ff</color>
      <color name="default">#ff000000</color>
 </resources>
于 2013-05-06T10:14:00.330 回答