日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Fragment onCreateView inflate注意事项 (整理)

發布時間:2024/4/15 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Fragment onCreateView inflate注意事项 (整理) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用Fragment很久了,今天忽然發現自己寫的東西,明明子Fragment是全屏,但是寬度只有那么一點點。其實這個問題的本質是inflate的方法的使用,之前也研究過但沒有留下記錄,在fragment使用上又暴漏出來了。直覺告訴我一定是哪塊出問題了,很快鎖定到onCreateView上。


在onCreateView里我們一般有兩種寫法:


方法1:

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView root = inflater.inflate(layoutId(), container, false);return root;}


方法2:

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView root = inflater.inflate(layoutId(), null);return root;}

這兩種都可以,但是如果采用第二種,就不會繼承到母布局的參數信息。來看下文檔:


方法2調用的inflate()方法如下:

/*** Inflate a new view hierarchy from the specified xml resource. Throws* {@link InflateException} if there is an error.* * @param resource ID for an XML layout resource to load (e.g.,* <code>R.layout.main_page</code>)* @param root Optional view to be the parent of the generated hierarchy.* @return The root View of the inflated hierarchy. If root was supplied,* this is the root View; otherwise it is the root of the inflated* XML file.*/public View inflate(int resource, ViewGroup root) {return inflate(resource, root, root != null);}

方法1調用的inflate()方法如下:

/*** Inflate a new view hierarchy from the specified xml resource. Throws* {@link InflateException} if there is an error.* * @param resource ID for an XML layout resource to load (e.g.,* <code>R.layout.main_page</code>)* @param root Optional view to be the parent of the generated hierarchy (if* <em>attachToRoot</em> is true), or else simply an object that* provides a set of LayoutParams values for root of the returned* hierarchy (if <em>attachToRoot</em> is false.)* @param attachToRoot Whether the inflated hierarchy should be attached to* the root parameter? If false, root is only used to create the* correct subclass of LayoutParams for the root view in the XML.* @return The root View of the inflated hierarchy. If root was supplied and* attachToRoot is true, this is root; otherwise it is the root of* the inflated XML file.*/public View inflate(int resource, ViewGroup root, boolean attachToRoot) {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 {return inflate(parser, root, attachToRoot);} finally {parser.close();}}


方法2調用的inflate()方法最終調用了方法1中調用的inflate()方法,即在inflate(int resource, ViewGroup root)中調用了inflate(int resource, ViewGroup root, boolean attachToRoot),所以直接看方法1調用的inflate()方法.

如果root不為空,attachToRoot為false的情況下,文檔說的很明白:If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML,即root只用來創建父布局的參數信息。而為true,則添加到父布局。因為我們控制fragment時要手動add/remove等,所以此處attachToRoot一定是false。

再反過來看,如果用了方法2,root一定要傳null,此時子view不會得到母view的布局信息,子view里一堆的match_parent也就毫無用途。

總結:90%的情況下,我們使用fragment此處一定要用方法1. 有一種情況,如創建dialog fragment時,不需要得到母布局參數信息可以用方法2.

補充:上文代碼里的layoutId()是個虛函數,用來在子類里實現。子類只需return R.layout.xxx就可以了,不必每個fragment都重寫onCreateView了。在onViewCreated里根據傳來的參數root去實例化各個控件。


原文URL:點擊打開鏈接

總結

以上是生活随笔為你收集整理的Fragment onCreateView inflate注意事项 (整理)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。