RadioGroup、RadioButton动态创建并定制icon显示
来源:互联网 发布:淘宝2017年双11营业额 编辑:程序博客网 时间:2024/05/17 22:10
由于原生的Radiobutton不能满足业务需求,所以需要自己定制icon图片,和默认选中某一项。需要自己代码动态实现。废话不多说,上代码:
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).relayMessage); 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>
效果如下图:
0 0
- RadioGroup、RadioButton动态创建并定制icon显示
- 自定义RadioGroup动态添加RadioButton,并获取选中radioButton的位置
- RadioGroup中动态添加RadioButton
- 动态在RadioGroup加载RadioButton
- Android 动态生成 radiobutton 解决 radiogroup 多行显示的问题 单选
- RadioGroup动态添加RadioButton,并且获得事件
- RadioGroup动态添加RadioButton,并且获得事件
- RadioGroup动态添加RadioButton,并且获得事件
- android中radioGroup动态添加radioButton
- RadioGroup的多行显示RadioButton
- radioGroup动态加载radiobutton且radiobutton没有button圆圈按钮
- RadioGroup中动态添加RadioButton,RadioButton状态紊乱
- RadioGroup && RadioButton
- RadioGroup,RadioButton
- RadioButton&RadioGroup
- RadioGroup+RadioButton
- RadioButton&RadioGroup
- RadioButton+RadioGroup
- openssh升级
- Webpack之Loaders
- android关于 LayoutInflater.from(context)中参数的问题
- Watch OS开发笔记
- 解决 platform-tools\adb.exe' and can be executed.
- RadioGroup、RadioButton动态创建并定制icon显示
- 理解 Thread.Sleep 函数
- matlab 直方图均衡化
- Javac 出现找不到符号,符号:类名**
- MM计算方案详解
- Nginx多站点虚拟主机实现单独启动停止php-fpm、单独控制权限设置
- matlab图像或数组填充函数padarray
- openlayers3画线画点
- 知识迁移能力-面试题37-两个链表的第一个公共结点