使用rightDrawable错误的位置复选框触摸动画

问题描述 投票:0回答:1

我使用rightDrawable财产使用自定义复选框RTL支持。

public class SRCheckBox extends AppCompatCheckBox {

    public SRCheckBox(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        if (isRTL()) {
            this.setButtonDrawable(null);
            int[] attrs = {android.R.attr.listChoiceIndicatorMultiple};
            TypedArray ta = context.getTheme().obtainStyledAttributes(attrs);
            Drawable rightDrawable = ta.getDrawable(0);
            this.setCompoundDrawablesWithIntrinsicBounds(null, null, rightDrawable, null);
        }
    }

}

但这里要说的是我现在面临的问题:请看看这个GIF

gif

正如你所看到的触摸动画是影响在左侧(文本),而不是动画中的复选框本身。

我也试过在XML

<CheckBox
    android:id="@+id/fastDecodeCB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@null" // this is causing the problem
    android:drawableRight="?android:attr/listChoiceIndicatorMultiple" />

但它看起来是相同的。有什么建议么?

android android-layout android-custom-view android-checkbox
1个回答
1
投票

您设置的复选框按钮设置为null有效去除,并设置正确的绘制。正确绘制响应的点击次数,但该复选框并不真正知道绘制的是按钮(你告诉它没有按键),因此它只是做你所看到的。

试着在你的自定义视图init方法如下。

private void init(Context context) {
    if (isRTL()) {
        // This will flip the text and the button drawable. This could also be set in XML.
        setLayoutDirection(LAYOUT_DIRECTION_RTL);
        int[] attrs = {android.R.attr.listChoiceIndicatorMultiple};
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs);
        Drawable rightDrawable = ta.getDrawable(0);
        this.setButtonDrawable(rightDrawable);
        ta.recycle(); // Remember to do this.
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.