Android Root layout invalid margin setting when dynamically loading sub-layouts

Source: Internet
Author: User

Most Android controls will have padding and layout_margin two properties, in general the difference is:

padding: The distance from the control's edge to the contents of the control .

margin: The distance the control is from the edge of its parent control .


Today, an experiment that dynamically loads sub-layouts by the root layout finds that the two properties in the sub-layout can be displayed as expected, but the padding set for the root layout does not have an effect on the child layout being loaded.


The code is as follows:

The root layout file is named Activity_main.xml and its XML file defines the following:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical"    android:padding= "8DP"  <!--this layout controls all from its edge 8DP--and    tools: Context= ". Mainactivity "></LinearLayout>


The above root layout adds the layout defined in the sub-layout table_layout.xml, which is defined by the XML file:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" wrap_content "    android:orientation=" vertical ">    <textview        Android:id= "@+id/tablelayout_tablename"        android:layout_width= "match_parent"        android:layout_height= " Wrap_content "        android:layout_margin=" 10DP "  <!--This control is 10DP-to-Android from the edge of the Table_layout layout        : Textsize= "30SP"/></linearlayout>

Code snippets that implement dynamic loading in the source:

Create a layout to host the table LinearLayout sublayout = (linearlayout) this.getlayoutinflater (). Inflate (r.layout.table_layout, NULL); /Fill table name Tablenametextview = ((TextView) Sublayout.findviewbyid (r.id.tablelayout_tablename)); Tablenametextview.settext ("tablename"); This.addcontentview (Sublayout, New Layoutparams (layoutparams.match_parent , layoutparams.wrap_content));

But when this code executes, the margin settings inside the table_layout layout are displayed normally, but the edges of the table_layout in the Activity_main layout are close to the activity_main edge, stating Activity_ The padding of main does not have its effect.


This question I tangled nearly 3 disappear, finally set the root local and sub-layout of the margin and padding also do not, set top, right, bottom, left also not, the final solution but let me feel very inconceivable:

Just add a layout to the root layout and use this layout as the root layout to dynamically load the sub-layout.

It is not known why the root layout of the exact same type will go wrong, perhaps the ' root ' layout has some special limitations.


The code after the modification is:

Activity_main.xml:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical"    tools:context= ". Mainactivity ">    <!--as long as you add a layout to replace with the layout is OK ... --    <linearlayout         android:id= "@+id/mainlayout"         android:layout_width= "Match_parent"         android:layout_height= "Match_parent"         android:orientation= "vertical"         android:paddingbottom= "8DP"        android:paddingleft= "8DP"         android:paddingright= "8DP" >     </linearlayout></ Linearlayout>


Source:

LinearLayout sublayout = (linearlayout) this.getlayoutinflater (). Inflate (r.layout.table_layout, NULL);// Fill table Name Tablenametextview = ((TextView) Sublayout.findviewbyid (r.id.tablelayout_tablename)); Tablenametextview.settext ("TableName"); LinearLayout mainlayout = (linearlayout) Findviewbyid (r.id.mainlayout);  Use this new "root layout" to load the sub-layout Mainlayout.addview (sublayout);



If reproduced please specify the source: http://blog.csdn.net/gophers?viewmode=contents


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.