RadioGroup、RadioButton动态创建并定制icon显示
2016-10-25 10:24
447 查看
由于原生的Radiobutton不能满足业务需求,所以需要自己定制icon图片,和默认选中某一项。需要自己代码动态实现。废话不多说,上代码:
R.drawable.relay_message_radio_selector 代码如下:
效果如下图:
private void setViewData() { radioGroup.removeAllViews(); int margin = ScreenUtil.dip2px(this, 14.0f); int marginLeft = ScreenUtil.dip2px(this, 20.0f); int paddingLeft = ScreenUtil.dip2px(this, 3.0f); RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); RadioGroup.LayoutParams layoutParams1 = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1); layoutParams.setMargins(marginLeft, margin, margin, margin); layoutParams1.setMargins(margin, 0, margin, 0); int defaultId = -1; for (int i = 0; i < messageList.size(); i++) { RadioButton rb = new RadioButton(this); rb.setMaxLines(2); rb.setPadding(paddingLeft, 0, 0, 0); rb.setText(messageList.get(i).rel 4000 ayMessage); rb.setButtonDrawable(android.R.color.transparent); rb.setTextColor(Color.parseColor("#555555")); rb.setButtonDrawable(null);// 去掉左边默认图标 rb.setCompoundDrawablePadding(margin); rb.setEllipsize(TextUtils.TruncateAt.END);// 结尾处打点显示 Drawable drawable = getResources().getDrawable(R.drawable.relay_message_radio_selector); drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); rb.setCompoundDrawables(drawable, null, null, null); // 手动生成id int generateId = generateViewId(); messages.put(generateId, messageList.get(i).relayMessage); rb.setId(generateId); if (i == 0) { defaultId = generateId; } TextView view = new TextView(this); view.setBackgroundColor(Color.parseColor("#dddddd")); radioGroup.addView(rb, layoutParams); radioGroup.addView(view, layoutParams1); } // 此处:默认选中第一个 radioGroup.check(defaultId); }
R.drawable.relay_message_radio_selector 代码如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!--设置自己的图片icon--> <item android:drawable="@drawable/relay_radio_unchecked" android:state_checked="false" /> <item android:drawable="@drawable/relay_radio_checked" android:state_checked="true" /> </selector>
效果如下图:
相关文章推荐
- Android 动态生成 radiobutton 解决 radiogroup 多行显示的问题 单选
- 动态布局中RadioGroup的RadioButton有时候不相互排斥的原因
- RadioGroup动态添加RadioButton,并且获得事件
- Android 代码中动态为RadioGroup添加RadioButton
- tabHost研究二 ,android应用中,大部分的软件都会采用把按钮放在底部显示(tabHost)。利用三个Radiobutton嵌套在RadioGroup 。
- Android中RadioGroup在代码中动态添加子控件RadioButton
- android中radioGroup动态添加radioButton
- 动态添加RadioGroup组件的RadioButton出现多选状况解决
- 动态布局中RadioGroup的RadioButton有时候不互斥的原因
- RadioGroup的多行显示RadioButton
- RadioGroup中动态添加RadioButton,RadioButton状态紊乱
- RadioGroup动态添加RadioButton,RadioButton设置默认选中第一个,点击其他的RadioButton第一个不再选中,第一个不再选中
- RadioGroup动态添加RadioButton,并且获得事件
- android 动态添加组件(RadioGroup 添加RadioButton和其他组件的一些问题)
- 动态在RadioGroup加载RadioButton
- 自定义RadioGroup动态添加RadioButton,并获取选中radioButton的位置
- Dialog中存在radiogroup动态添加radiobutton的ID问题
- 使用RadioButton 和RadioGroup
- ext4.1动态生成多个checkboxgroup(或者radiogroup),并且有toolbar操作、
- Android之单选控件RadioGroup,RadioButton使用