我正在做一个库存管理Android应用程序,其中我以编程方式并动态创建行以放入我的xml定义的tablelayout。对于每个项目,我创建一个包含2个视图的表格:包含清单项目名称的文本视图和包含3个单选按钮的radiogroup:存在,缺失和需要的修复。 tablelayout中的每行下面还有一个线性布局,它包含一个textview和一个edittext,但该部分没有任何问题。将一个3按钮radiogroup的一个单选按钮移动到其Tablerow中的第二行
这是我的截图显示它目前的样子: https://preview.ibb.co/k8a7Yw/Screenshot_20171011_170618.png
眼下,单选按钮显示我的手机屏幕上的罚款(以XML为tablelayout设置shrinkcolumn = 0,这样的文字后第三个单选按钮不会被切断),但是项目名称会被挤压。我想要发生的事情是将最后一个单选按钮(需要修复)向下移动一行,但将其保存在radiogroup和row中,因此我不必通过创建从radiogroup继承的自定义类来让它向右切换。
当前代码:
public class PMR {
String result;
String caption;
String repairString;
EditText repairEditText;
RadioButton pRadioButton;
RadioButton mRadioButton;
RadioButton rRadioButton;
public PMR(){
}
public PMR(final TableLayout tableLayout, final TableRow tableRow,
final Context context, String caption){
this.caption=caption;
result="incomplete";
repairString=null;
final RadioGroup radioGroup=new RadioGroup(context);
radioGroup.setTag("incomplete");
radioGroup.setOrientation(RadioGroup.HORIZONTAL);
tableRow.addView(radioGroup);
pRadioButton = new RadioButton(context);
pRadioButton.setText("Present");
pRadioButton.setId(generateViewId());
final int pId=pRadioButton.getId();
mRadioButton = new RadioButton(context);
mRadioButton.setText("Missing");
mRadioButton.setId(generateViewId());
final int mId=mRadioButton.getId();
rRadioButton = new RadioButton(context);
rRadioButton.setText("Repairs Needed");
rRadioButton.setId(generateViewId());
final int rId=rRadioButton.getId();
radioGroup.addView(pRadioButton);
radioGroup.addView(mRadioButton);
radioGroup.addView(rRadioButton);
final LinearLayout textLayout=new LinearLayout(context);
textLayout.setOrientation(LinearLayout.HORIZONTAL);
tableLayout.addView(textLayout);
textLayout.setTag("Text Row");
final TextView labelText = new TextView(context);
labelText.setText("");
textLayout.addView(labelText);
repairEditText = new EditText(context);
repairEditText.setEnabled(false);
repairEditText.setText("");
textLayout.addView(repairEditText);
final String prefix="Notes: ";
radioGroup.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged (RadioGroup group,
int checkedId){
if (checkedId!=rId &&
repairEditText.getText().toString()!=null){
repairString=repairEditText.getText().toString();
}
if (checkedId == pId) {
result="Present";
repairEditText.setText("");
repairEditText.setEnabled(false);
labelText.setText("");
radioGroup.setTag("pTag");
} else if (checkedId == mId) {
result="Missing";
repairEditText.setText("");
repairEditText.setEnabled(false);
labelText.setText("");
radioGroup.setTag("mTag");
} else if (checkedId == rId) {
result="Repairs Needed";
if (repairString!=null){
repairEditText.setText(repairString);
}
repairEditText.setEnabled(true);
repairEditText.requestFocus();
labelText.setText(prefix);
radioGroup.setTag("rTag");
}
else {
result = "incomplete";
radioGroup.setTag("incomplete");
}
}
});
}
}
随着tablelayout XML:
<TableLayout
android:id="@+id/tableLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle"
android:shrinkColumns="0"
>
</TableLayout>
这基本上就是我试图让我的tablerows看起来像: https://image.ibb.co/i5qrtw/goodtablerowpmr.png
https://stackoverflow.com/questions/11891105/have-horizontal-radiobuttons-wrap-if-too-long屏幕 – HomeIsWhereThePcIs
该帖子的两个回复均未回答提出的问题;第一个不适用于radiogroups,第二个只会在额外的按钮不足的时候将其移除。我希望我的行是统一的,并且在按钮外观上都有2个按钮。 – devander
第一个答案使用扩展RadioGroup的引用的MultiLineRadioGroup类。 – HomeIsWhereThePcIs