xiaoyan_12
xiaoyan_12
采纳率84.9%
2012-10-16 03:51 阅读 6.8k

Android:如何添加一个自定义的按钮状态

例如,默认按钮在它的状态和背景图片之间具有以下依赖:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal" />
    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/btn_default_normal_disable" />
    <item android:state_pressed="true" 
        android:drawable="@drawable/btn_default_pressed" />
    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/btn_default_selected" />
    <item android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal" />
    <item android:state_focused="true"
        android:drawable="@drawable/btn_default_normal_disable_focused" />
    <item
        android:drawable="@drawable/btn_default_normal_disable" />
</selector>

我如何定义自己的自定义状态(如android:state_custom), 然后可以使用它动态地改变按钮的外观吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    csh_34 csh_34 2012-10-16 05:48
     <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="food">
            <attr name="state_fried" format="boolean" />
            <attr name="state_baked" format="boolean" />
        </declare-styleable>
    </resources>
    
    public FoodButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    private static final int[] STATE_FRIED = {R.attr.state_fried};
    private static final int[] STATE_BAKED = {R.attr.state_baked};
    
    private boolean mIsFried = false;
    private boolean mIsBaked = false;
    
    public void setFried(boolean isFried) {mIsFried = isFried;}
    public void setBaked(boolean isBaked) {mIsBaked = isBaked;}
    Then override function "onCreateDrawableState":
    
    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
        if (mIsFried) {
            mergeDrawableStates(drawableState, STATE_FRIED);
        }
        if (mIsBaked) {
            mergeDrawableStates(drawableState, STATE_BAKED);
        }
        return drawableState;
    }
    

    文件 "res/drawable/food_button.xml":

    <?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/com.mydomain.mypackage">
    <item
        app:state_baked="true"
        app:state_fried="false"
        android:drawable="@drawable/item_baked" />
    <item
        app:state_baked="false"
        app:state_fried="true"
        android:drawable="@drawable/item_fried" />
    <item
        app:state_baked="true"
        app:state_fried="true"
        android:drawable="@drawable/item_overcooked" />
    <item
        app:state_baked="false"
        app:state_fried="false"
        android:drawable="@drawable/item_raw" />
    </selector>
    
    点赞 1 评论 复制链接分享

相关推荐