快速提问。我有两个表的布局。一个表是标题,第二个是表本身。 我将它们分成两个空隙,然后分别按预期工作,但如果我尝试它们,则只显示标题。
我错过了什么吗?
更新1
void addTable();有效,因为它打开时显示数字键盘。因此添加视图无效
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:id="@+id/table1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TableLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:id="@+id/table2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_light"></TableLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
&#13;
public class SingleGate extends AppCompatActivity {
CheckBox checkBox, checkBox1;
TextView tv, tv1, qty, qty1, txt1, txt2, txt3;
Button addBtn, minusBtn;
EditText input;
TableLayout tableLayout;
Integer column1, column2, column3;
TableLayout table, table2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_gate);
Toast.makeText(this, "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
table = (TableLayout) findViewById(R.id.table1);
table2 = (TableLayout) findViewById(R.id.table2);
addHeader();
addTable();
}
public void addHeader() {
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
table.setLayoutParams(lp);
table.setStretchAllColumns(true);
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.6f);
TableRow row = new TableRow(this);
TextView hdr1 = new TextView(this);
TextView hdr2 = new TextView(this);
TextView hdr3 = new TextView(this);
TextView hdr4 = new TextView(this);
//ADD FIRST CELL
hdr1.setText("A");
hdr1.setGravity(Gravity.CENTER);
hdr1.setMinimumHeight(100);
hdr1.setBackground(getResources().getDrawable(R.drawable.cell_shape));
row.addView(hdr1, cellLp);
//ADD SECOND CELL
hdr2.setText("NR");
hdr2.setGravity(Gravity.CENTER);
hdr2.setMinimumHeight(100);
hdr2.setBackground(getResources().getDrawable(R.drawable.cell_shape));
row.addView(hdr2, cellLp);
//ADD THIRD CELL
hdr3.setText("B");
hdr3.setGravity(Gravity.CENTER);
hdr3.setMinimumHeight(100);
hdr3.setBackground(getResources().getDrawable(R.drawable.cell_shape));
row.addView(hdr3, cellLp);
//ADD FOURTH CELL
hdr4.setText("NR");
hdr4.setGravity(Gravity.CENTER);
hdr4.setMinimumHeight(100);
hdr4.setBackground(getResources().getDrawable(R.drawable.cell_shape));
row.addView(hdr4, cellLp);
table.addView(row, rowLp);
}
public void addTable() {
// TableLayout table2 = (TableLayout) findViewById(R.id.table2);
// Java. You succeed!
TableLayout.LayoutParams lp2 = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
table2.setLayoutParams(lp2);
table2.setStretchAllColumns(true);
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
TableRow.LayoutParams cellLp2 = new TableRow.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
for (int r = 1; r < 12; ++r) {
TableRow row2 = new TableRow(this);
TextView tbl1 = new TextView(this);
EditText tbl2 = new EditText(this);
TextView tbl3 = new TextView(this);
EditText tbl4 = new EditText(this);
// This is very useful if someone already made some InputFilter. It overrides android:maxlength
// in xml file, so we need to add LengthFilter this way.
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(2);
//ADD FIRST CELL
tbl1.setText("A" + r);
tbl1.setGravity(Gravity.CENTER);
tbl1.setMinimumHeight(100);
tbl1.setBackground(getResources().getDrawable(R.drawable.cell_shape));
row2.addView(tbl1, cellLp2);
//ADD SECOND CELL
tbl2.setHint("0");
tbl2.setGravity(Gravity.CENTER);
tbl2.setInputType(InputType.TYPE_CLASS_NUMBER);
tbl2.setFilters(filterArray);
tbl2.setBackground(getResources().getDrawable(R.drawable.table_cell));
row2.addView(tbl2, cellLp2);
//ADD THIRD CELL
tbl3.setText("B" + r);
tbl3.setGravity(Gravity.CENTER);
tbl3.setMinimumHeight(100);
tbl3.setBackground(getResources().getDrawable(R.drawable.cell_shape));
row2.addView(tbl3, cellLp2);
//ADD FOURTH CELL
tbl4.setHint("0");
tbl4.setGravity(Gravity.CENTER);
tbl4.setInputType(InputType.TYPE_CLASS_NUMBER);
tbl4.setFilters(filterArray);
tbl4.setMinimumHeight(100);
tbl4.setBackground(getResources().getDrawable(R.drawable.table_cell));
row2.addView(tbl4, cellLp2);
table2.addView(row2, rowLp);
}
}
}
&#13;
更新2
添加了截图