0

我有一个自定义ImageButton类,ToggleButton's根据本教程How to add a custom button state模仿选中状态。

当我有一个可绘制的状态列表作为android:src属性时,一切正常,但自定义状态不适用于该ImageButton's android:background属性。

这是我的代码:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.ImageButton;

public class CheckableImageButton extends ImageButton implements Checkable {

    private static final int[] STATE_CHECKED = {R.attr.state_checked};

    private boolean mChecked = false;

    public CheckableImageButton(Context context) {
        super(context);
    }

    public CheckableImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);

        if(mChecked){
            mergeDrawableStates(drawableState, STATE_CHECKED);
        }

        return drawableState;
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void setChecked(boolean checked) {
        mChecked = checked;
        refreshDrawableState();
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }
}

以及布局 XML 中的相关片段:

<com.my.package.view.CheckableImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:background="@drawable/header_button_bg"
    android:padding="5dp"
    android:src="@drawable/menu_button"
    tools:ignore="ContentDescription" />

状态列表可绘制:

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

    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#ff000000" />

            <gradient android:angle="-90" android:endColor="#d2914e" android:startColor="#906434" />

            <corners android:radius="5dp" />
        </shape>
    </item>
    <item app:state_checked="true">
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#ff000000" />

            <gradient android:angle="-90" android:endColor="#d2914e" android:startColor="#906434" />

            <corners android:radius="5dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#ff000000" />

            <gradient android:angle="-90" android:endColor="#4f5b6c" android:startColor="#345b75" />

            <corners android:radius="5dp" />
        </shape>
    </item>

</selector>
4

1 回答 1

0

Another wonderful trait of eclipse probably..

When I tried to revert my code to the last working version by hand (the state list drawable in the android:src tag), it produced the same error. I reverted from the SVN repo, it worked. Then I made the exact same changes as before, character by character, no difference, and voilá, it works now!

So that's it, the code in the question is fully functional.

于 2012-11-22T15:37:13.910 回答