android - 使用 rightDrawable 在错误位置上的 CheckBox 触摸动画
问题描述
我正在使用使用 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
如您所见,触摸动画影响的是左侧(文本),而不是复选框本身的动画。
我也试过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" />
但它看起来一样。有什么建议么?
解决方案
您正在将复选框按钮设置为 null,从而有效地删除它并设置正确的可绘制对象。正确的drawable响应点击,但复选框并不真正知道drawable是按钮(你告诉它没有按钮),所以它只是你看到的。
在您的自定义视图中尝试以下 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.
}
}
推荐阅读
- php - 更改 PHP 梨邮件中的电子邮件标题
- python - 为什么我的标签没有显示在 tkinter 中?
- python - Setuptools:使用源文件夹中的 setup.py 从“项目”导入
- javascript - 使用javascript在Adobe中根据大小删除空白页
- c - 在 C 中:头文件和编译 main 函数时遇到问题
- python - Python中的列表是否存在“无关”值
- node.js - 如何在端口 80 上部署第二台服务器(听 EADDRINUSE :::80)
- vb.net - 如何实现我用 Public Subs 制作的自定义 DLL
- python - 将自定义分隔符添加回 csv?
- scala - Scala 缺少扩展函数的参数类型匿名函数的参数类型必须是完全已知的。(SLS 8.5)