Android中RadioGroup的使用

在安卓中为了给在几个选项中选择其中某个选项,需要用到Radiogroup

2、为了增加灵活行,想要在Java代码中动态加载Radio 这就涉及到一个问题,Radio的样式应该怎样修改

RadioGroup的代码

<RadioGroup
    android:id="@+id/rbgAttrSelect"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

RadioButton的添加

mRadioGroup = mView.findViewById(R.id.rbgAttrSelect);
for (EnumAdvancedType type : mTabList) {
    RadioButton radioButton = new RadioButton(getActivity());
    radioButton.setChecked(true);
    radioButton.setText(getString(type.getNameResId()));
    radioButton.setId(type.getVal());
    mRadioGroup.addView(radioButton);
}
mRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
    if (checkedId != mOldId) {
        mOldId = checkedId;
        changeFragment(checkedId);
    }
});

 其中fragment存在hashmap中与tabid对应

修改RadioButton的样式

通过Java代码可以修改文字大小 控件高度等,但是好像WrapContent这个字段不好用

radioButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18);
radioButton.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

只要使用setHeight(Viewgroup.LayoutParams.WRAP_CONTENT)就会出现所有的Radio都消失的问题

未完

 

posted on   摆渡人19966  阅读(440)  评论(0编辑  收藏  举报

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示