代码生成RadioButton问题

String[] items = new String[]{"语音一", "语音二", "语音三"};
rdo_sel_lang.removeAllViews();
for(int i=0;i<items.length;i++){
RadioButton rdoBtn = new RadioButton(this);

rdoBtn.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));//去除默认按钮

RadioButton和CheckBox都是Android app中常用的Widget,它们派生于CompoundButton,允许使用者自行设置背景和按钮的样式,不过,有时我们仅希望简单的设置一个有状态的背景,并隐藏其默认样式。可是,当我们调用setButtonDrawable(null)或setButtonDrawable(0)时,却发现完全没有效果。原来,CompoundButton的setButtonDrawable的代码实现中屏蔽了null或resid为0的Drawable,迫使我们必须传入有效的Drawable对象。

点击打开链接

Drawable nav_up=getResources().getDrawable(android.R.drawable.btn_radio);   //使用替换的图片
nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight()); //Drawable对象必须调用setBounds方法
rdoBtn.setCompoundDrawables(null, null, nav_up, null); //设置自定义图片位置
rdoBtn.setText(items[i]);
rdo_sel_lang.addView(rdoBtn);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值