2012-07-22 46 views
1

我有以下可繪製文件button.xml:如何創建XML drawables項的選擇器?

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

兩個項目是XML可繪製兩個單獨的工作:button_down.xml和button_up.xml。

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
    <corners android:radius="10dp" /> 
    <gradient 
     android:angle="90" 
     android:centerColor="#FFBD21" 
     android:endColor="#FFAE18" 
     android:gradientRadius="60" 
     android:startColor="#FFD060" 
     android:type="linear" 
     android:useLevel="false" /> 
</shape> 



<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
    <corners android:radius="10dp" /> 
    <gradient 
     android:angle="90" 
     android:centerColor="#FFBD21" 
     android:endColor="#FFD060" 
     android:gradientRadius="60" 
     android:startColor="#FFAE18" 
     android:type="linear" 
     android:useLevel="false" /> 
</shape> 

但是,當作爲項目使用選擇器時,我得到以下錯誤。是否允許XML drawables作爲項目到選擇器?

org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable 
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:178) 
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:867) 
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:804) 
at com.android.layoutlib.bridge.impl.ResourceHelper.getDrawable(ResourceHelper.java:208) 
at android.content.res.BridgeTypedArray.getDrawable(BridgeTypedArray.java:774) 
at android.view.View.<init>(View.java:2785) 
at android.widget.TextView.<init>(TextView.java:449) 
at android.widget.Button.<init>(Button.java:108) 
at android.widget.Button.<init>(Button.java:104) 
at sun.reflect.GeneratedConstructorAccessor17.newInstance(Unknown Source) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
at java.lang.reflect.Constructor.newInstance(Unknown Source) 
at android.view.LayoutInflater.createView(LayoutInflater.java:586) 
at android.view.BridgeInflater.onCreateView(BridgeInflater.java:86) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678) 
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:131) 
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:739) 
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:711) 
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:742) 
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:711) 
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:742) 
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:711) 
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:742) 
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:711) 
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:742) 
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:711) 
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:742) 
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:711) 
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:742) 
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:711) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:372) 
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:321) 
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:322) 
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325) 
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:372) 
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1317) 
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1071) 
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.onXmlModelChanged(GraphicalEditorPart.java:1033) 
at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.xmlModelChanged(LayoutEditor.java:317) 
at com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor$XmlModelStateListener.modelChanged(AndroidXmlEditor.java:1405) 
at org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.fireModelChanged(AbstractStructuredModel.java:553) 
at org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.internalModelChanged(AbstractStructuredModel.java:887) 
at org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.changedModel(AbstractStructuredModel.java:382) 
at org.eclipse.wst.xml.core.internal.document.DOMModelImpl.changedModel(DOMModelImpl.java:167) 
at org.eclipse.wst.sse.ui.StructuredTextEditor.savedModel(StructuredTextEditor.java:2687) 
at org.eclipse.wst.sse.ui.StructuredTextEditor.doSave(StructuredTextEditor.java:1690) 
at com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor.doSave(AndroidXmlEditor.java:524) 
at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.doSave(LayoutEditor.java:148) 
at org.eclipse.ui.internal.SaveableHelper$2.run(SaveableHelper.java:151) 
at org.eclipse.ui.internal.SaveableHelper$5.run(SaveableHelper.java:274) 
at org.eclipse.jface.operation.ModalContext.runInCurrentThread(ModalContext.java:464) 
at org.eclipse.jface.operation.ModalContext.run(ModalContext.java:372) 
at org.eclipse.jface.window.ApplicationWindow$1.run(ApplicationWindow.java:759) 
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70) 
at org.eclipse.jface.window.ApplicationWindow.run(ApplicationWindow.java:756) 
at org.eclipse.ui.internal.WorkbenchWindow.run(WorkbenchWindow.java:2579) 
at org.eclipse.ui.internal.SaveableHelper.runProgressMonitorOperation(SaveableHelper.java:282) 
at org.eclipse.ui.internal.SaveableHelper.runProgressMonitorOperation(SaveableHelper.java:261) 
at org.eclipse.ui.internal.SaveableHelper.savePart(SaveableHelper.java:156) 
at org.eclipse.ui.internal.EditorManager.savePart(EditorManager.java:1357) 
at org.eclipse.ui.internal.WorkbenchPage.savePart(WorkbenchPage.java:3328) 
at org.eclipse.ui.internal.WorkbenchPage.saveEditor(WorkbenchPage.java:3341) 
at org.eclipse.ui.internal.SaveAction.run(SaveAction.java:76) 
at org.eclipse.jface.action.Action.runWithEvent(Action.java:498) 
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584) 
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501) 
at org.eclipse.jface.action.ActionContributionItem$6.handleEvent(ActionContributionItem.java:452) 
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) 
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1003) 
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3910) 
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3503) 
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2405) 
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2369) 
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2221) 
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:500) 
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) 
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:493) 
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149) 
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:113) 
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:194) 
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) 
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) 
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:368) 
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:559) 
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:514) 
at org.eclipse.equinox.launcher.Main.run(Main.java:1311) 
+0

之所以如此怪異,我也有類似的選擇和我接受的項目可繪製。也許嘗試清理一個項目來強制重新編譯資源? – azgolfer 2012-07-22 02:14:59

+0

@azgolfer我曾經有PNG圖像和那些工作。然後發現了標籤並開始使用它。將看到新項目會發生什麼。 – Salsero69 2012-07-22 02:21:25

回答

1

這可能不是你想要的答案,但我有這個button.xml,它工作。該形狀包含在<item>標籤本身中。但是我也有另一個可繪製的xml,它引用<item>標籤中的非PNG可繪製,並且它也起作用。祝你好運。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true" > 
    <shape> 
     <solid 
      android:color="#565656" /> 
     <stroke 
      android:width="1dp" 
      android:color="#171717" /> 
     <corners 
      android:radius="3dp" /> 
     <padding 
      android:left="8dp" 
      android:top="2dp" 
      android:right="8dp" 
      android:bottom="2dp" /> 
    </shape> 
</item> 
<item android:state_checked="true" > 
    <shape> 
     <solid 
      android:color="#343434" /> 
     <stroke 
      android:width="3dp" 
      android:color="#767676" /> 
     <corners 
      android:radius="3dp" /> 
     <padding 
      android:left="8dp" 
      android:top="2dp" 
      android:right="8dp" 
      android:bottom="2dp" /> 
    </shape> 
</item>   
<item> 
    <shape> 
     <gradient 
      android:startColor="#565656" 
      android:endColor="#262626" 
      android:angle="270" /> 
     <stroke 
      android:width="1dp" 
      android:color="#171717" /> 
     <corners 
      android:radius="4dp" /> 
     <padding 
      android:left="8dp" 
      android:top="2dp" 
      android:right="8dp" 
      android:bottom="2dp" /> 
    </shape> 
</item> 

+0

這是一個很好的選擇,它的工作;並沒有打破我的架構。所以我可以像這樣離開它。謝謝。 – Salsero69 2012-07-22 02:37:06