RadioButton在Android4.4调用setButtonDrawable(null)无效的问题
来源:互联网 发布:windows pip install 编辑:程序博客网 时间:2024/06/06 20:39
今天遇到一个问题,在 Android 4.4 系统下,Java 代码布局中调用 RadioButton.setButtonDrawable(null)
无效,而在 xml 布局中设置元素属性 android:button="@null"
有效,并且在 Android 5.0+ 系统上调用 Java 代码布局也有效。
初步分析,应该是系统差异导致的,因此直接比较 Android 4.4 和 Android 5.0 系统代码中 RadioButton.setButtonDrawable()
方法的差异。
Android 4.4 版本的代码如下:
public void setButtonDrawable(Drawable d) { if (d != null) { if (mButtonDrawable != null) { mButtonDrawable.setCallback(null); unscheduleDrawable(mButtonDrawable); } d.setCallback(this); d.setVisible(getVisibility() == VISIBLE, false); mButtonDrawable = d; setMinHeight(mButtonDrawable.getIntrinsicHeight()); } refreshDrawableState();}
可以看见,在 Android 4.4 系统中,如果在 setButtonDrawable()
方法中传入 null,是没有任何作用的,仍然会显示 RadioButton 默认的样式。
而 XML 布局里面将 android:button=”@null”,会在 RadioButton 调用构造方法初始化的时候,就将预设样式的值设置为 null,这就是为什么 Java 布局代码设置无效而 XML 代码设置有效的原因。
而 Android 5.0 的代码如下:
public void setButtonDrawable(Drawable d) { if (mButtonDrawable != d) { if (mButtonDrawable != null) { mButtonDrawable.setCallback(null); unscheduleDrawable(mButtonDrawable); } mButtonDrawable = d; if (d != null) { d.setCallback(this); d.setLayoutDirection(getLayoutDirection()); if (d.isStateful()) { d.setState(getDrawableState()); } d.setVisible(getVisibility() == VISIBLE, false); setMinHeight(d.getIntrinsicHeight()); applyButtonTint(); } }}
可以看到,系统会先比较传入的值和预设样式的值(mButtonDrawable)是否相等,这里传入为 null,和预设值当然不相等,然后就将传入的值赋值给预设样式,这样,当 RadioButton 显示到界面上的时候,就没有预设样式了。
知道原因后,现在来看看如何解决 Android 4.4 系统上调用 RadioButton.setButtonDrawable(null)
代码无效的问题。
RadioButton 预设样式的值 mButtonDrawable
是在抽象父类 CompoundButton
中定义的,因此,我们要做的就是拿到这个属性字段,然后将其设置为 null 即可,代码很简单:
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { try { Field field = radioButton.getClass().getSuperclass().getDeclaredField("mButtonDrawable"); field.setAccessible(true); field.set(radioButton, null); } catch (Exception e) { e.printStackTrace(); }}
Android 4.X 系统代码和 Android 5.X 的代码差异很大,有很多坑都是因为系统代码差异造成的,真希望能早日摒弃对 Android 4.X 的适配,哈哈。
- RadioButton在Android4.4调用setButtonDrawable(null)无效的问题
- RadioButton调用setButtonDrawable(null)无效
- Android CheckBox setButtonDrawable(null) 在 4.x 版本中无效
- 隐藏RadioButton, CheckBox的drawable图片 setButtonDrawable
- 隐藏RadioButton, CheckBox图片 setButtonDrawable
- 隐藏RadioButton, CheckBox图片 setButtonDrawable
- 在确认UIAlertView后,调用presentViewController无效的问题
- setButtonDrawable()的兼容问题
- Android4.4打开系统相册返回Null问题
- Android4.4打开系统相册返回Null问题
- android4.4以上 webview加在图片的缩放问题
- 在AppCompatActivity中Fragment的addToBackStack(null)为何无效?
- transaction.addToBackStack(null);无效问题
- asp 用split出现null无效的问题
- CountDownTimer空指针(NULL)与Cancel无效的问题
- android4.4 Launcher3 修改配置文件default_workspace.xml无效的解决方法
- android4.4 Launcher3 修改配置文件default_workspace.xml无效的解决方法
- 安卓CountDownTimer的cancel方法在onTick中调用无效的问题
- Druid:一个用于大数据实时处理的开源分布式系统之怎么用
- Git的诞生
- HTML学习一 表头的学习
- Android 6.0+在运行时请求权限
- 【Linux】利用fork()创建多个线程
- RadioButton在Android4.4调用setButtonDrawable(null)无效的问题
- spring 依赖问题一 (小问题, 经常遇到)
- 第6章创建函数-----------(创建函数、定义函数、使用函数、返回值)
- 136. Single Number
- 感受
- 为什么word打字后面的字会消失?
- docker(11):alpinelinux安装openjre
- AngularJS2.0 教程系列(一) (赞)
- stuu快捷键