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