改变backgroundcolor后对Button的效果

时间:2013-05-06 09:43:08

标签: android button background click

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

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

我试过这样:

在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

可能是我的问题?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

如错误所示,您需要在android:drawable的{​​{1}}标记中加入<item/>属性。将您的button_bg.xml更改为以下内容:

button_bg.xml

在res / values /文件夹中创建<?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