Usage of layoutparams in Android

Source: Internet
Author: User

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; }}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.