【发布时间】:2018-03-23 16:51:11
【问题描述】:
我正在做一个库存管理 Android 应用程序,我在其中以编程方式动态创建行以放入我的 xml 定义的表格布局中。对于每个项目,我创建一个包含 2 个视图的表行:一个包含库存项目名称的文本视图和一个包含 3 个单选按钮的单选按钮:存在、缺失和需要维修。在 tablelayout 的每一行下面还有一个 linearlayout,其中包含一个 textview 和一个 edittext,但该部分没有任何问题。
这是我的屏幕截图,显示了它目前的样子: https://preview.ibb.co/k8a7Yw/Screenshot_20171011_170618.png
现在,单选按钮在我的手机屏幕上显示正常(在为 tablelayout 设置 xml 中的 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>
这基本上就是我想让我的表格看起来像的样子: https://image.ibb.co/i5qrtw/goodtablerowpmr.png
【问题讨论】:
-
该帖子的两个回复都没有回答提出的问题;第一个不适用于无线电组,第二个仅在空间不足时将附加按钮向下移动。我希望我的行是统一的,并且在 1 个按钮的外观上都有 2 个按钮。
-
第一个答案使用引用的 MultiLineRadioGroup 类,它扩展了 RadioGroup。
-
他们改变了顺序。 multilineradiogroup 类仅在空间不足时才进行包装。无论左侧的文本有多长,我都希望它设置按钮为 2 比 1 的位置。
-
我猜你可以通过改变 MultiLineRadioGroup 类中的布局逻辑来达到同样的效果。 if(nextBottom > maxBottom){ // 如果当前按钮将超出此列的边框...您可以使用以下内容进行更改: if (x>2 && x%2==1)
标签: android layout radio-button tablerow radio-group