-1

我想更改按钮颜色,所以我使用了以下选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true" android:color="#FFFF00"/>
    <item android:state_pressed="true" android:color="#FFFF00"/>

    <item>
        <shape>
            <gradient android:startColor="#ff00ff00" android:endColor="#bb00ff00" android:angle="270" />
            <stroke android:width="1dp" android:color="#bb00ff00" />
            <corners android:radius="3dp" />
            <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
        </shape>
    </item>

</selector>

并像这样使用它

<Button
    android:id="@+id/SODEOD"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="SOD/EOD" 
    android:background="@drawable/firstbuttoncolorstyle" 
    />

但这会导致应用程序崩溃

知道为什么会发生这种情况,以及如何解决它,这就是日志猫

05-15 15:25:06.491: E/AndroidRuntime(666): FATAL EXCEPTION: main
05-15 15:25:06.491: E/AndroidRuntime(666): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appnetics/com.appnetics.TryNewLayoutActivity}: android.view.InflateException: Binary XML file line #92: Error inflating class <unknown>
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.os.Looper.loop(Looper.java:123)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-15 15:25:06.491: E/AndroidRuntime(666):  at java.lang.reflect.Method.invokeNative(Native Method)
05-15 15:25:06.491: E/AndroidRuntime(666):  at java.lang.reflect.Method.invoke(Method.java:521)
05-15 15:25:06.491: E/AndroidRuntime(666):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-15 15:25:06.491: E/AndroidRuntime(666):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-15 15:25:06.491: E/AndroidRuntime(666):  at dalvik.system.NativeStart.main(Native Method)
05-15 15:25:06.491: E/AndroidRuntime(666): Caused by: android.view.InflateException: Binary XML file line #92: Error inflating class <unknown>
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.view.LayoutInflater.createView(LayoutInflater.java:513)
05-15 15:25:06.491: E/AndroidRuntime(666):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-15 15:25:06.491: E/AndroidRuntime(666):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.app.Activity.setContentView(Activity.java:1647)
05-15 15:25:06.491: E/AndroidRuntime(666):  at com.appnetics.TryNewLayoutActivity.onCreate(TryNewLayoutActivity.java:24)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-15 15:25:06.491: E/AndroidRuntime(666):  ... 11 more
05-15 15:25:06.491: E/AndroidRuntime(666): Caused by: java.lang.reflect.InvocationTargetException
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.widget.Button.<init>(Button.java:65)
05-15 15:25:06.491: E/AndroidRuntime(666):  at java.lang.reflect.Constructor.constructNative(Native Method)
05-15 15:25:06.491: E/AndroidRuntime(666):  at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.view.LayoutInflater.createView(LayoutInflater.java:500)
05-15 15:25:06.491: E/AndroidRuntime(666):  ... 24 more
05-15 15:25:06.491: E/AndroidRuntime(666): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/firstbuttoncolorstyle.xml from drawable resource ID #0x7f020001
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.content.res.Resources.loadDrawable(Resources.java:1697)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.view.View.<init>(View.java:1885)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.widget.TextView.<init>(TextView.java:327)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.widget.Button.<init>(Button.java:69)
05-15 15:25:06.491: E/AndroidRuntime(666):  ... 28 more
05-15 15:25:06.491: E/AndroidRuntime(666): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:165)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
05-15 15:25:06.491: E/AndroidRuntime(666):  at android.content.res.Resources.loadDrawable(Resources.java:1694)
05-15 15:25:06.491: E/AndroidRuntime(666):  ... 32 more
4

2 回答 2

4

尝试这个:

<?xml version="1.0" encoding="utf-8"?>

<item android:state_focused="true">
    <shape>
        <solid android:color="#FFFF00"/>
        <stroke android:width="1dp" android:color="#bb00ff00" />
        <corners android:radius="3dp" />
        <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
    </shape>
</item>
<item android:state_pressed="true">
    <shape>
        <solid android:color="#FFFF00"/>
        <stroke android:width="1dp" android:color="#bb00ff00" />
        <corners android:radius="3dp" />
        <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
    </shape>
</item>
<item>
    <shape>
        <gradient android:startColor="#ff00ff00" android:endColor="#bb00ff00" android:angle="270" />
        <stroke android:width="1dp" android:color="#bb00ff00" />
        <corners android:radius="3dp" />
        <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
    </shape>
</item>

它应该工作...

你需要有一个形状在每个<item>...

于 2012-05-15T13:01:08.670 回答
0

tab-bg_selected.xml

   <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
        android:shape="rectangle">

        <corners  android:radius="8dp" />
        <gradient android:startColor="#FEE5AC" android:centerColor="#FEF1B5"
                    android:endColor="#FCD59C" android:angle="-90" />

    </shape>

tab_bag_unselected.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- <corners  android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp"/> -->

    <gradient android:startColor="#0000FF" android:centerColor="#0000FF" 
    android:endColor="#0000FF"
        android:angle="135" />

</shape>

tab_bg_selecter.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Active tab -->
    <item android:drawable="@drawable/tab_bg_selected" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>

    <!-- Inactive tab -->
    <item android:drawable="@drawable/tab_bg_unselected" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>

    <!--
    <item android:state_pressed="true" android:drawable="@android:drawable/alert_dark_frame" />
     Selected tab (using d-pad)
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@color/transparent" />
    -->

</selector>
于 2012-05-15T12:58:07.353 回答