The margin setting is invalid when the root layout of Android dynamically loads the sub layout.

Source: Internet
Author: User

Most Android controls have two attributes: padding and layout_margin. The difference is generally:

Padding:Control contentThe distance from the control edge.

Margin: ControlAway from its parent ControlThe distance from the edge.


Today, an experiment was conducted to dynamically load sub-la s from the root layout. The results show that the two attributes in the sub-layout can be displayed as expected, however, the padding set for the root layout does not have any effect on the loaded sub-layout.


The Code is as follows:

The root layout file name is activity_main.xml. The xml file defines the following content:

 
  
Tools: context = ". MainActivity">
 


In the above root layout, the layout defined in table_layout.xml is added. The definition content of this xml file is:

     
  
   
Android: textSize = "30sp"/>
  
 

Code segment for dynamic loading in source code:

// Create the layout LinearLayout subLayout = (LinearLayout) this. getLayoutInflater (). inflate (R. layout. table_layout, null); // enter the table name tableNameTextView = (TextView) subLayout. findViewById (R. id. tableLayout_tableName); tableNameTextView. setText ("tablename"); this. addContentView (subLayout, new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT ));

However, after the above code is executed, the margin settings in the table_layout layout will be displayed normally, but the edge of table_layout in the activity_main layout is closely tied to the edge of activity_main, which means that the padding of activity_main has no effect.


I am entangled in nearly three problems, and finally set the root part and the sub-layout of the margin and padding, respectively, set top, right, bottom, left neither, the final solution made me feel incredible:

You only need to add a layout in the root layout, and use this layout as the root layout to dynamically load the sub-layout.

I don't know why the root layout with identical types will go wrong. Maybe the 'root' layout has some special restrictions.


The modified code is:

Activity_main.xml:

     
      
       
  
 


Source code:

LinearLayout subLayout = (LinearLayout) this. getLayoutInflater (). inflate (R. layout. table_layout, null); // enter the table name tableNameTextView = (TextView) subLayout. findViewById (R. id. tableLayout_tableName); tableNameTextView. setText ("tablename"); LinearLayout mainLayout = (LinearLayout) findViewById (R. id. mainLayout); // load the sublayout mainLayout using the newly added "root layout. addView (subLayout );



If reprint please indicate the source: http://blog.csdn.net/gophers? Viewmode = contents


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.