RecyclerView android:layout_width="match_parent" 無效


使用RecyclerView 時,在xml文件中設置寬度match_parent無效。

1 View view = mInflater.from(mContext).inflate(R.layout.item_recycler_view, parent, false);

對於:inflate

1 public View inflate(int resource, ViewGroup root, boolean attachToRoot) {  
2     if (DEBUG) System.out.println("INFLATING from resource: " + resource);  
3     XmlResourceParser parser = getContext().getResources().getLayout(resource);  
4     try {  
5         return inflate(parser, root, attachToRoot);  
6     } finally {  
7         parser.close();  
8     }  
9 } 

即調用了:

public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) 

其方法對於理解后兩個參數作用的代碼:

 1 ViewGroup.LayoutParams params = null;  
 2   
 3 if (root != null) {  
 4     if (DEBUG) {  
 5         System.out.println("Creating params from root: " +  
 6                 root);  
 7     }  
 8     // Create layout params that match root, if supplied  
 9     params = root.generateLayoutParams(attrs);  
10     if (!attachToRoot) {  
11         // Set the layout params for temp if we are not  
12         // attaching. (If we are, we use addView, below)  
13         temp.setLayoutParams(params);  
14     }  
15 }  

調用其方法:

1、傳入了ViewGroup root參數,則會從root中得到由layout_width和layout_height組成的LayoutParams

2、如果attachToRoot設置為false的話,就會對我們加載的視圖View設置該LayoutParams。

所以RecyclerView 如果采用平常方式去inflate,寬度不會顯示全,設置layout_width="match_parent" 無效。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM