动态添加radiobutton的样式问题

monakatyking 2016-08-11 10:17:54
如题:
我在xml文件里面
<RadioGroup
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="57dp"
android:orientation="horizontal"
android:background="#fcfcfc"
>

<RadioButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:text="hehe"
android:textColor="#000"
/>
<RadioButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:text="haha"
android:textColor="#000"
/>

</RadioGroup>

这样写,权重值有起作用

但是如果是动态添加

<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/radioButton"
>

<style name="radioButton">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center</item>
<item name="android:button">@null</item>
</style>


RadioGroup group = (RadioGroup) findViewById(R.id.bottom);
String[] titles = {"测试1","测试2","测试3"};
int size = titles.length;
if(titles != null){
for(int i = 0; i< size; i++){
RadioButton radioButton = (RadioButton) LayoutInflater.from(this).inflate(R.layout.tabmenu_radiobutton,null);
radioButton.setId(i);
radioButton.setText(titles[i]);
group.addView(radioButton);

}
}

就变成

我想动态添加的radiobutton,能等比分配空,请问有大神能帮忙解决吗?
...全文
334 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
monakatyking 2016-08-16
  • 打赏
  • 举报
回复
楼上两位提的意见我都试过,没有用 不过我已经解决了,还是谢谢了
fsilence璐豪 2016-08-11
  • 打赏
  • 举报
回复
RadioButton 获取LayoutParameter 将width 设置为0 weight设置1试试
  • 打赏
  • 举报
回复
得到屏幕的宽度再除以radiobutton的个数

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧