首页 文章

自定义按钮上的自定义属性不显示

提问于
浏览
0

我扩展了Button小部件,以便能够应用多个自定义属性 .
其中一个属性是我在创建按钮时尝试应用于其背景的滤镜 . 这是行不通的 . (参见下面的屏幕截图和代码)
我尝试在相同的代码位置直接设置背景颜色,它确实改变了背景颜色,但它不是我需要的,因为我使用自己的按钮PNG .

到目前为止有2个问题:

  • 未应用滤色器

  • 自定义按钮已偏移,剪裁且无法单击

第二个按钮使用普通按钮,它按预期定位,可以点击 . 第二个屏幕截图显示确实选择了正确的颜色,并且可以更改代码中该点的按钮背景颜色 .

码:

public class MyButton extends Button {
    private int backGroundColor;

    public MyButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.getTheme().obtainStyledAttributes(
            attrs, 
            R.styleable.MyButton, 
            defStyle, 
            0);

        try {
            Resources res = getResources();
            switch( a.getInteger(R.styleable.MyButton_type, 0) ) {
            case 0: 
                backGroundColor = res.getColor(R.color.Black); break;
            case 1: 
                backGroundColor = res.getColor(R.color.Red); break;
            case 2: 
                backGroundColor = res.getColor(R.color.DimGray); break;
            }
            getBackground().setColorFilter(backGroundColor, Mode.MULTIPLY);
            //setBackgroundColor(backGroundColor)
        } finally {
            a.recycle();
        }
    }
    public MyButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public MyButton(Context context) {
        this(context, null, 0);
    }
}

我使用的XML:

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <com.example.test.MyButton
        android:id="@+id/btn1"
        android:text="BTN1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="14sp"
        android:textColor="@color/Blue"
        android:padding="2dp"
        android:layout_margin="4dp"
        android:background="@drawable/key_selector"
        app:type="RedButton"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="14sp"
        android:textColor="@color/Blue"
        android:padding="2dp"
        android:layout_margin="4dp"
        android:background="@drawable/key_selector"
        android:id="@+id/btn2"
        android:text="BTN2"/>
</LinearLayout>

setColorFilter()结果
enter image description here
的屏幕截图

setBackgroundColor()的截图结果
enter image description here

EDIT 这是我用于普通和按下状态的选择器XML .

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

    <item
        android:drawable="@drawable/key1_pressed"
        android:state_pressed="true"/>

    <item 
        android:drawable="@drawable/key1"/>


</selector>

1 回答

  • 1

    自定义按钮已偏移,剪裁且无法单击

    这是由于你使用了构造函数 . Button 类确实在它的构造函数之间进行链接,但它没有将 0 传递给最后一个构造函数作为样式(来自第二个构造函数,即使用的那个),它传递了一个内部样式(最终使得视觉 Button ) . 如果你通过:

    this(context, attrs, android.R.attr.buttonStyle);
    

    Button 应该没问题 .

    未应用滤色器

    进行上述校正后,设置滤色器的代码应该没有问题 . 当你看到两个按钮都将应用了过滤器时(因为它们具有相同的位图(I 'm assuming you use an image)). This is happening because the drawables of the same type share a constant state. You can read more from Romain Guy' s说明here

    getBackground().mutate().setColorFilter(backGroundColor, Mode.MULTIPLY);
    

    让我知道这是否解决了问题(根据我的理解):

    public static class MyButton extends Button {
    
        private int backGroundColor;
        private StateListDrawable mSld;
        private PorterDuffColorFilter mColorFilter;
        private boolean mHandled = false;
    
        public MyButton(Context context, AttributeSet attrs, int defStyle) {
            // ...
            try {
                //...
                mSld = (StateListDrawable) getBackground();
                mColorFilter = new PorterDuffColorFilter(backGroundColor,
                        Mode.MULTIPLY);
                mSld.setColorFilter(mColorFilter);
            } finally {
                a.recycle();
            }
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            if (!mHandled) {
                final Drawable current = mSld.getCurrent();
                current.mutate();
                current.setColorFilter(mColorFilter);
                mHandled = true;
            }
            super.onDraw(canvas);
        }
    
    }
    

相关问题