0

我正在尝试将 aTableRow分成 4 个相等的部分。第一个是 a TextView,最后三个是RadioGroups。我已经成功地将它们TableRow分成了这些相等的部分。但是,每一行都是动态构建的,每行最多可能有 5RadioButtonsRadioGroup. 当在每个显示和功能中构建一个TableRow5时,它们都被挤压在一起。发生这种情况时,有没有办法让多行,但仍然保持在同一部分的同一部分?当给定适当的参数时,基本上就像 a会变成多行?RadioButtonsRadioGroupRadioButtonsRadioGroupTableRowTextView

本案例的 Java 代码:

RadioButton rb1, rb2, rb3, rb4, rb5;
RadioGroup rg;
TextView tv;
TableLayout tl = (TableLayout) this.findviewById(R.string.tl);
LayoutInflater inflater = getLayoutInflater();

//set up TableRow and TextView
TableRow tr = (TableRow)inflater.inflate(R.layout.tablerow3q, tl, false);
tr.setBackgroundColor(Color.LTGRAY);
tv = (TextView)tr.findViewById(R.id.tv);
tv.setText("Some text");

//loop sets up 1 RadioGroup per increment, each with 5 RadioButtons
for(int i = 0; i < 3; i++) {
    rb1 = new RadioButton(this);
    rb2 = new RadioButton(this);
    rb3 = new RadioButton(this);
    rb4 = new RadioButton(this);
    rb5 = new RadioButton(this);

    rb1.setText("test1");
    rb2.setText("test2");
    rb3.setText("test3");
    rb4.setText("test4");
    rb5.setText("test5");

    //without these layouts, only four buttons are displayed, the other is cut off
    rb1.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
    rb2.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
    rb3.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
    rb4.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
    rb5.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);

    //add RadioButtons to correct RadioGroup based on loop increment
    if(i == 0) {
        rg1 = (RadioGroup)tr.findViewById(R.id.rg1);
        rg1.addView(rb1);
        rg1.addView(rb2);
        rg1.addView(rb3);
        rg1.addView(rb4);
        rg1.addView(rb5);
    }
    if(i == 1) {
        rg1 = (RadioGroup)tr.findViewById(R.id.rg2);
        rg1.addView(rb1);
        rg1.addView(rb2);
        rg1.addView(rb3);
        rg1.addView(rb4);
        rg1.addView(rb5);
    }
    if(i == 2) {
        rg1 = (RadioGroup)tr.findViewById(R.id.rg3);
        rg1.addView(rb1);
        rg1.addView(rb2);
        rg1.addView(rb3);
        rg1.addView(rb4);
        rg1.addView(rb5);
    }
}
tl.addView(tr);

XML 布局TableRow正在被夸大:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:id="@+id/tv"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_margin="1dip"
        android:layout_weight="0.25"
        android:background="#ffffff"/>
    <RadioGroup
        android:id="@+id/rg1"
        android:layout_width="0dip"
        android:orientation="horizontal"
        android:layout_margin="1dip"
        android:layout_weight="0.25"
        android:background="#ffffff"/>
    <RadioGroup
        android:id="@+id/rg2"
        android:layout_width="0dip"
        android:orientation="horizontal"
        android:layout_margin="1dip"
        android:layout_weight="0.25"
        android:background="#ffffff"/>
    <RadioGroup
        android:id="@+id/rg3"
        android:layout_width="0dip"
        android:orientation="horizontal"
        android:layout_margin="1dip"
        android:layout_weight="0.25"
        android:background="#ffffff"/>
</TableRow>
4

1 回答 1

2

RadioGroup 扩展了 LinearLayout,它不包装其内容。不过,有几个选项可以解决这个问题:

  1. 复制RadioGroup该类的源代码并对其进行修改以扩展不同类型的 Layout,例如 aTableLayoutRelativeLayout. 您需要管理单选按钮的排列方式。

  2. 动态构建一个嵌套TableLayout并使用OnClickListeners 来处理选择/取消选择各种单选按钮。

于 2012-09-23T21:56:12.780 回答