With fragment for a long time, today suddenly found himself writing things, obviously son fragment is full screen, but the width is only a little bit. In fact, the essence of this problem is the use of inflate methods, have been studied before, but have left records, in the fragment use and burst out. My gut tells me there must be something wrong, and soon it's locked up on the Oncreateview.
In Oncreateview we generally have two ways of writing, Method 1:
@Override publiconCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub false); return root; }
方法2
@Override publiconCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub null); return root; }
Both of these are possible, but if you take the second one, you will not inherit the parameter information from the parent layout. Take a look at the documentation:
/** * Inflate a new view hierarchy from theSpecified XML resource. Throws * {@link inflateexception}ifThere isAnError. * * @param resource ID forAn XML layout resource toLoad (e.g., * <code>R.layout.main_page</code>) * @param root Optional view toBe theParent of theGenerated hierarchy. * @returnThe root View of theInflated hierarchy. If Root is supplied, * this is theRoot View; otherwiseit is theRoot of theinflated * XMLfile. */Public View inflate (int resource, ViewGroup root) {returnInflate (resource, root, root! = NULL); }
/** * Inflate a new view hierarchy from theSpecified XML resource. Throws * {@link inflateexception}ifThere isAnError. * * @param resource ID forAn XML layout resource toLoad (e.g., * <code>R.layout.main_page</code>) * @param root Optional view toBe theParent of theGenerated hierarchy (if* <em>attachToRoot</em> is true),or ElseSimply an Object that* Provides aSet ofLayoutparams values forRoot of theReturned * Hierarchy (if<em>attachToRoot</em> is false.) * @param attachtoroot Whether theInflated hierarchy should be attached to* theRoot parameter? Iffalse, Root isOnly used toCreate the* Correct Subclass ofLayoutparams for theRoot viewinch theXML. * @returnThe root View of theInflated hierarchy. If Root was supplied and* Attachtoroot is true, this isRoot otherwiseit is theRoot of* theInflated XMLfile. */Public View inflate (int resource, ViewGroup root,BooleanAttachtoroot) {Final Resources res = GetContext (). Getresources ();if(DEBUG) {LOG.D (TAG,"inflating from resource: \" "+ res.getresourcename (Resource) +"\" ("+ integer.tohexstring (Resource) +")"); } final Xmlresourceparser parser = res.getlayout (Resource);Try{returnInflate (parser, Root, attachtoroot); } finally {parser.close (); } }
Method 2 Finally calls Method 1, which is inflate (int resource, ViewGroup root, Boolean attachtoroot), So look directly at method 1. If root is not empty and Attachtoroot is false, the document is very clear: if False, Root is only used to create the correct subclass of Layoutpara Ms for the root view in the XML, that is, root is used only to create parameter information for the parent layout. True to add to the parent layout. Because we have to manually add when we control fragment, the attachtoroot here must be false.
In turn, if you use the method 2,root must be null, at this time the child view will not get the layout information of the parent view, a bunch of match_parent in the child view is useless.
Summary: 90% of the case, we use fragment here must use Method 1. In one case, such as creating dialog fragment, you do not need to get the parent layout parameter information in Method 2.
Add: LayoutID () in the above code is a virtual function that is used in subclasses. Subclass just return r.layout. * So you don't have to rewrite oncreateview for every fragment. In onviewcreated, the individual controls are instantiated according to the parameter root.
Fragment Oncreateview Inflate Precautions