A control should use the Layoutparams type of its parent control. Therefore, a tablevow should use Tablelayout.params.
So, take a TableRow as an example:
New TableRow (context); Tablerow.setlayoutparams (new tablelayout.layoutparams ( TableLayout.LayoutParams.MATCH_ PARENT, TableLayout.LayoutParams.MATCH_PARENT,1.0f));
When I do not specify the type of Layoutparams, TableRow does not automatically adapt to the screen length. Because TableRow don't know the size of tablelayout. So at this point, even if the weight set is not so.
[android-developers] re:possible bug in Tablelayout
Romain Guy Mon, 19:57:16-0800
Your code is *not* equivalent to the XML. You are using the wronglayoutparams everywhere. A widget must has the layoutparams of its*parent*. Therefore, the rows must has Tablelayout.layoutparams, thetextviews must has Tablerow.layoutparams and the tablelayout m Usthave Framelayout.layoutparams.
Here is your code, corrected, and it works just fine.package com.test;import android.app.activity;import android.graphics. Color;import Android.os.bundle;import Android.view.view;import Android.widget.framelayout;import Android.widget.tablelayout;import Android.widget.tablerow;import Android.widget.textview;public class Test extends Activity {/** Called when the activity is first created. */@Override public void onCreate (Bundle sa Vedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (Buildtableviewfromsource ()); Private View Buildtableviewfromsource () {framelayout.layoutparams ptable = new Framelayout.layout Params (TableRow.LayoutParams.FILL_PARENT, TABLEROW.LAYOUTPA Rams. Fill_parent); Tablelayout table = new Tablelayout (this); Table.setbackgroundcolor (color.red); Table.setlayoutparaMS (ptable); TableRow rowtop = new TableRow (this); Tablelayout.layoutparams prowtop = new Tablelayout.layoutparams (tablelayout.layoutparams.f Ill_parent, TableLayout.LayoutParams.WRAP_CONTENT); Prowtop.weight = 1; Rowtop.setbackgroundcolor (Color.Blue); TextView txt = new TextView (this); Txt.settext ("Top Content"); Rowtop.addview (TXT, new Tablerow.layoutparams (TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); TableRow Rowbottom = new TableRow (this); Rowbottom.setbackgroundcolor (Color.green); TextView txtbottom = new TextView (this); Txtbottom.settext ("Bottom Content"); Tablelayout.layoutparams prowbottom = new Tablelayout.layoutparams (TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT); Rowbottom.addview (Txtbottom, New Tablerow.layoutparams (TableRow.LayoutParams.FILL_PARE NT, TableRow.LayoutParams.WRAP_CONTENT)); Table.addview (Rowtop, prowtop); Table.addview (Rowbottom, Prowbottom); return table; }}