如果我在代码中动态添加复选框,我应该如何使用自定义复选框? (关于 Java 代码而不是 XML 文件。)我正在关注 this tutorial ,但使用它无法实现我的目标。
例如,我有一个 tableLayout
,我想为每个新行添加一个复选框。
谢谢大家
最佳答案
如果你想添加自定义复选框试试这个:
StateListDrawable stateList = new StateListDrawable();
int statePressed = android.R.attr.state_pressed;
int stateChecked = android.R.attr.state_checked;
stateList.addState(new int[] {-stateChecked}, new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.check_3)));
stateList.addState(new int[] {stateChecked}, new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.check_1)));
stateList.addState(new int[] {statePressed}, new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.check_2)));
final CheckBox box = new CheckBox(this);
box.setButtonDrawable(stateList);
关于Android:将自定义复选框动态添加到 tableLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2891748/