Sunday, 2 November 2014

Adding TableLayout and TableRow to LinearLayout doesn't show



I'm trying to programatically adding a TableRow to a LinearLayout, but it's not showing up. Here's the XML code:



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/linearLayout">


</LinearLayout>


And here's the Java code:



LinearLayout linear = (LinearLayout) findViewById(R.id.linearLayout);
Button btn = new Button(this);
btn.setText("ASDF");
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linear.addView(btn);

TableLayout table = new TableLayout(this);
table.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
TableRow row = new TableRow(this);
row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));

Button btn2 = new Button(this);
btn2.setText("Inside");
btn2.setLayoutParams(new TableLayout.LayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0f)));

row.addView(btn2);
table.addView(row);
linear.addView(table);


The Button with "ASDF" shows up every time, but btn2 with "Inside" never shows. Any help would be appreciated!


No comments:

Post a Comment