【发布时间】:2014-12-23 17:01:18
【问题描述】:
我想以编程方式将视图添加到我的父视图中,我的视图是 RadioButton。我还想为在RadioGroup 中添加的视图设置一个 ID。但是当我这样做时,它会给我一个错误:mRadioButton.setId(321);
Reports two types of problems:
Supplying the wrong type of resource identifier. For example, when calling Resources.getString(int id), you should be passing R.string.something, not R.drawable.something.
Passing the wrong constant to a method which expects one of a specific set of constants. For example, when calling View#setLayoutDirection, the parameter must be android.view.View.LAYOUT_DIRECTION_LTR or android.view.View.LAYOUT_DIRECTION_RTL.
我不知道为什么它会给我这个错误。
我发现一种解决方案是创建MyRadioButton 类,其中extends RadioButton 可以添加变量int MYID;,然后为此视图添加getters 和setters。
但这是一种解决方法,有人知道这个问题的解决方案吗?
谢谢!
编辑:
另一种解决方法是使用setTag()
(我已将它用于动态添加ImageView 的水平视图,它可以帮助我检测点击了哪一个)
【问题讨论】:
标签: java android view android-studio