【问题标题】:Android set android:state_checked="false" in codeAndroid 在代码中设置 android:state_checked="false"
【发布时间】:2015-12-30 01:52:23
【问题描述】:

我想使用下面的代码创建一个选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/sh_radio_icon_checked" />
    <item android:state_checked="false" android:drawable="@drawable/sh_radio_icon_unchecked" />
</selector>

到目前为止,我已经设法添加了第一项,如下所示:

StateListDrawable drawable = new StateListDrawable();
int[] sFocusedSelected = {android.R.attr.state_checked};
Drawable dFocusedSelected = getResources().getDrawable(R.drawable.sh_radio_icon_checked);
drawable.addState(sFocusedSelected, dFocusedSelected);

但是由于没有 state_unchecked 常量,我是否要添加 state_checked=false?

【问题讨论】:

标签: android


【解决方案1】:

注意“android.R.attr.state_checked”开头的“-”(减号/连字符)

sld.addState(new int[] {-android.R.attr.state_checked  },  greyD);

Android : How to update the selector(StateListDrawable) programmatically

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 2012-10-01
    • 2010-10-07
    • 2021-08-14
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多