我正在尝试创建一个具有自定义布局和 2 个主题(白天和黑夜)的 Button。
按钮布局具有背景图像(可绘制)和文本(字符串)。
主题必须在可绘制(我有两个 PNG)和文本颜色方面有所不同。
我的方法如下:
在按钮布局中将背景可绘制对象设置为选择器:
这是drawable/btn_basket_calls.xml
<item android:drawable="@drawable/btn_basket_calls_pressed" android:state_focused="true" android:state_pressed="true" /> <item android:drawable="@drawable/btn_basket_calls_pressed" android:state_focused="false" android:state_pressed="true" /> <item android:drawable="@drawable/btn_basket_calls_selected" android:state_focused="true" /> <item android:drawable="@drawable/btn_basket_calls_disabled" android:state_enabled="false" /> <item android:drawable="?attr/btn_basket_calls_default" android:state_focused="false" android:state_pressed="false" />
“默认”drawable 是对 attrs.xml 中定义的属性“btn_basket_calls_default”的引用,如下所示:
AppTheme 在 styles.xml 中有两个子主题:
@drawable/btn_basket_calls_d_default
@drawable/btn_basket_calls_n_default
AFAIK 它应该可以工作 :) 但是该项目可以正常运行,但会出现运行时异常:
E/AndroidRuntime( 1154): FATAL EXCEPTION: main
E/AndroidRuntime( 1154): java.lang.RuntimeException: Unable to start activity ComponentInfo{a.daynighttest/a.daynighttest.DayNightTestActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
E/AndroidRuntime( 1154): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
E/AndroidRuntime( 1154): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime( 1154): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 1154): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime( 1154): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1154): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 1154): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 1154): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1154): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 1154): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 1154): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 1154): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1154): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
E/AndroidRuntime( 1154): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
E/AndroidRuntime( 1154): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
E/AndroidRuntime( 1154): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
E/AndroidRuntime( 1154): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
E/AndroidRuntime( 1154): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
E/AndroidRuntime( 1154): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
E/AndroidRuntime( 1154): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
E/AndroidRuntime( 1154): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
E/AndroidRuntime( 1154): at android.app.Activity.setContentView(Activity.java:1657)
E/AndroidRuntime( 1154): at a.daynighttest.DayNightTestActivity.onCreate(DayNightTestActivity.java:13)
E/AndroidRuntime( 1154): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 1154): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
E/AndroidRuntime( 1154): ... 11 more
E/AndroidRuntime( 1154): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime( 1154): at java.lang.reflect.Constructor.constructNative(Native Method)
E/AndroidRuntime( 1154): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
E/AndroidRuntime( 1154): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
E/AndroidRuntime( 1154): ... 22 more
E/AndroidRuntime( 1154): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/btn_basket_calls.xml from drawable resource ID #0x7f020000
E/AndroidRuntime( 1154): at android.content.res.Resources.loadDrawable(Resources.java:1697)
E/AndroidRuntime( 1154): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
E/AndroidRuntime( 1154): at android.view.View.<init>(View.java:1951)
E/AndroidRuntime( 1154): at android.view.View.<init>(View.java:1899)
E/AndroidRuntime( 1154): at android.view.ViewGroup.<init>(ViewGroup.java:286)
E/AndroidRuntime( 1154): at android.widget.LinearLayout.<init>(LinearLayout.java:120)
E/AndroidRuntime( 1154): ... 25 more
E/AndroidRuntime( 1154): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #31: <item> tag requires a 'drawable' attribute or child tag defining a drawable
E/AndroidRuntime( 1154): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:167)
E/AndroidRuntime( 1154): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
E/AndroidRuntime( 1154): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
E/AndroidRuntime( 1154): at android.content.res.Resources.loadDrawable(Resources.java:1694)
E/AndroidRuntime( 1154): ... 30 more
有关详细信息,请参阅附加的完整 Eclipse 项目。
谢谢!:)