我试图用动画创建一个切换按钮我使用切换按钮和4 XML来执行此操作: 这是我的主要活动,切换按钮在其中:
<ToggleButton
android:id="@+id/toggle"
android:layout_width="144dp"
android:layout_height="144dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/btn_toggle_bg"
android:checked="true"
android:onClick="OnPressToggle" />
这是我的btn_toggle_bg。 xml有背景值:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+android:id/background" android:drawable="@android:color/transparent" />
<item android:id="@+android:id/toggle" android:drawable="@drawable/btn_toggle" />
</layer-list>
</LinearLayout>
这是我的btn_toggle。 xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/btn_switch_on" />
<item android:state_checked="false" android:drawable="@drawable/btn_switch_off" />
</selector>
这是新的日志猫
09-26 03:25:36.856: E/AndroidRuntime(19979): FATAL EXCEPTION: main
09-26 03:25:36.856: E/AndroidRuntime(19979): Process: com.soheil.prolight, PID: 19979
09-26 03:25:36.856: E/AndroidRuntime(19979): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.soheil.prolight/com.soheil.prolight.MainActivity}: android.view.InflateException: Binary XML file line #37: Error inflating class android.widget.ToggleButton
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.os.Handler.dispatchMessage(Handler.java:102)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.os.Looper.loop(Looper.java:136)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-26 03:25:36.856: E/AndroidRuntime(19979): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 03:25:36.856: E/AndroidRuntime(19979): at java.lang.reflect.Method.invoke(Method.java:515)
09-26 03:25:36.856: E/AndroidRuntime(19979): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-26 03:25:36.856: E/AndroidRuntime(19979): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-26 03:25:36.856: E/AndroidRuntime(19979): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
09-26 03:25:36.856: E/AndroidRuntime(19979): at dalvik.system.NativeStart.main(Native Method)
09-26 03:25:36.856: E/AndroidRuntime(19979): Caused by: android.view.InflateException: Binary XML file line #37: Error inflating class android.widget.ToggleButton
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
09-26 03:25:36.856: E/AndroidRuntime(19979): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
09-26 03:25:36.856: E/AndroidRuntime(19979): at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
09-26 03:25:36.856: E/AndroidRuntime(19979): at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.view.LayoutInflater.inflate(Native Method)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-26 03:25:36.856: E/AndroidRuntime(19979): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.app.Activity.setContentView(Activity.java:1929)
09-26 03:25:36.856: E/AndroidRuntime(19979): at com.soheil.prolight.MainActivity.onCreate(MainActivity.java:84)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.app.Activity.performCreate(Activity.java:5231)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-26 03:25:36.856: E/AndroidRuntime(19979): ... 12 more
09-26 03:25:36.856: E/AndroidRuntime(19979): Caused by: java.lang.reflect.InvocationTargetException
09-26 03:25:36.856: E/AndroidRuntime(19979): at java.lang.reflect.Constructor.constructNative(Native Method)
09-26 03:25:36.856: E/AndroidRuntime(19979): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
09-26 03:25:36.856: E/AndroidRuntime(19979): ... 28 more
09-26 03:25:36.856: E/AndroidRuntime(19979): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/btn_toggle_bg.xml from drawable resource ID #0x7f020005
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.content.res.Resources.loadDrawable(Resources.java:2096)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.content.res.XResources$XTypedArray.getDrawable(XResources.java:965)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.view.View.<init>(View.java:3554)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.widget.TextView.<init>(TextView.java:623)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.widget.Button.<init>(Button.java:107)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.widget.CompoundButton.<init>(CompoundButton.java:68)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.widget.ToggleButton.<init>(ToggleButton.java:51)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.widget.ToggleButton.<init>(ToggleButton.java:64)
09-26 03:25:36.856: E/AndroidRuntime(19979): ... 31 more
09-26 03:25:36.856: E/AndroidRuntime(19979): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag LinearLayout
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:933)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
09-26 03:25:36.856: E/AndroidRuntime(19979): at android.content.res.Resources.loadDrawable(Resources.java:2092)
09-26 03:25:36.856: E/AndroidRuntime(19979): ... 39 more
答案 0 :(得分:1)
您的可绘制名称不能以数字开头:
将所有这些重命名为,例如:
<item android:drawable="@drawable/d1" android:duration="50"/>
<item android:drawable="@drawable/d2" android:duration="50"/>
...
以下是您应遵循的命名规则:
答案 1 :(得分:0)
将您的“btn_toggle_bg.xml”文件从“res\drawable-hdpi”移动到“res\drawable”文件夹。 它将解决问题