【问题标题】:How to fix Dynamic RadioButtons Programmatically?如何以编程方式修复动态单选按钮?
【发布时间】:2021-04-09 17:38:12
【问题描述】:

我正在尝试修复 RadioButton 布局:

我帮助radioButton修复成这样:

这是我的代码:

    ScrollView sv = new ScrollView(this);
    ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    sv.addView(ll);

        RadioButton[] rb = new RadioButton[10];
        RadioGroup rg = new RadioGroup(this); 
        rg.setOrientation(RadioGroup.HORIZONTAL); 
        for(int j=0; j<((JSONArray) value).length(); j++){
               rb[j]  = new RadioButton(this);
               rb[j].setText("" + ((JSONArray) value).getString(j));
               rb[j].setId(i + 100);
               rg.addView(rb[j]);
        }

      ll.addView(rg);
      this.setContentView(sv);

【问题讨论】:

标签: android radio-button


【解决方案1】:

LinearLayout 用于将一个项目显示在另一个项目旁边,因此它不是这里的最佳选择。

简单的方法是Gridlayout,它专门用于在网格中显示项目。

如果您有 很多 单选按钮,您也可以使用 RecyclerviewGridlayoutManager,但我想这有点过头了。

您也可以尝试将ConstraintLayoutFlow 一起使用,以保持您的视图层次结构浅,并在不同宽度的设备上很好地扩展。

最后一句话:尽可能使用 xml,而不是夸大视图,它让生活变得更加轻松。 (不适用于作曲)

【讨论】:

    猜你喜欢
    • 2019-03-25
    • 2021-12-19
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多