Cor*_*lio 21 android radio-button android-layout
我正在尝试以编程方式将一组RadioButtons添加到RadioGroup,如下所示:
for (int i = 0; i < RANGE; i++) {
RadioButton button = new RadioButton(this);
button.setId(i);
button.setText(Integer.toString(i));
button.setChecked(i == currentHours); // Select button with same index as currently selected number of hours
button.setButtonDrawable(Color.TRANSPARENT);
button.setBackgroundResource(R.drawable.selector);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
((RadioGroup)view.getParent()).check(view.getId());
currentHours = view.getId();
}
});
radioGroupChild.addView(button);
}
Run Code Online (Sandbox Code Playgroud)
我需要将drawable按钮设置为null(我只想在我的背景上添加文本).在XML中手动执行此操作android:button="@null"
非常好,但我不想对每个单选按钮进行硬编码.我尝试过这样做,button.setButtonDrawable(null)
但它没有改变任何东西.
任何帮助是极大的赞赏!
geo*_*sey 43
你需要设置一个空StateListDrawable
的drawable.所以Java相当于android:button="@null"
:
radioButton.setButtonDrawable(new StateListDrawable());
Run Code Online (Sandbox Code Playgroud)
你应该做这个:
button.setBackgroundDrawable(null);
Run Code Online (Sandbox Code Playgroud)
如果你的drawable有一个对selector的引用,你可以通过选择器xml使它透明:
<item android:drawable="@android:color/transparent" />
Run Code Online (Sandbox Code Playgroud)
因为你可能找到了解决方案;)
setButtonDrawable(getResources().getDrawable(android.R.color.transparent))
应该可以。
只有这个对我有用。
归档时间: |
|
查看次数: |
13137 次 |
最近记录: |