RecyclerView 中的 item 如何居中問題
阿新 • • 發佈:2019-01-30
一個很簡單的Item佈局,我只要讓它由上而下排列,文字居中
然後程式碼這樣寫:很標準的使用方式吧?
問題來了,無論你怎麼設定item中各元素的layout_width是match_parent,都無法讓文字居中,為什麼?
這個問題還得從android的LayoutInflater.from(context).inflate(...)原始碼下手,
inflater在inflate一個xml時,需要知道parent的型別,才能生成對應的LayoutParams,才可以把xml根節點的attrs(如layout_width)讀進去,程式碼如下:
如果parent傳進去為null,生成的View的LayoutParams為null,在RecyclerView.addView時,發現LayoutParams為null,則生成預設的LayoutParams,
所以無論無論你怎麼寫,最外層的LinearLayout寬度為WRAP_CONTENT,如果那三個點的寬度為6dp,那麼整個View的寬度也為6dp,所以無法居中。
所以要解決照這個問題需要在inflate的時候將parent傳進去,如:
同時,最後一個引數設定成false,如果不填該引數則拋異常,說先要removeAllViews()
衍生1,為何ListView加進去就是MATCH_PARENT的?
因為AbsListView重寫的generateDefaultLayoutParams方法為
衍生2,為何高度只能用minHeight控制?
同理,layout.xml根節點的attrs屬性沒被寫到LayoutParams中!所以使用minHeight來控制高度的做法是可笑的!你所要做的是在inflate時把parent傳進去!
RecyclerView自定義LayoutManager,打造不規則佈局
http://blog.csdn.net/qibin0506/article/details/52676670
<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/item_0" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="8dp" android:textColor="@color/grayDark" android:textSize="@dimen/font_2xbig" /> </LinearLayout>
然後程式碼這樣寫:很標準的使用方式吧?
recyclerView = (RecyclerView) findViewById(R.id.recyclerView); recyclerView.setHasFixedSize(true); recyclerView.addItemDecoration(new DividerGridItemDecoration(this)); recyclerView.setItemAnimator(new DefaultItemAnimator()); LinearLayoutManager manager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); // GridLayoutManager manager = new GridLayoutManager(this, 2); recyclerView.setLayoutManager(manager); recyclerView.setAdapter(adapter...);
問題來了,無論你怎麼設定item中各元素的layout_width是match_parent,都無法讓文字居中,為什麼?
這個問題還得從android的LayoutInflater.from(context).inflate(...)原始碼下手,
inflater在inflate一個xml時,需要知道parent的型別,才能生成對應的LayoutParams,才可以把xml根節點的attrs(如layout_width)讀進去,程式碼如下:
// android.view.LayoutInflater public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) { synchronized (mConstructorArgs) { ... // Temp is the root view that was found in the xml final View temp = createViewFromTag(root, name, inflaterContext, attrs); ViewGroup.LayoutParams params = null; if (root != null) { if (DEBUG) { System.out.println("Creating params from root: " + root); } // Create layout params that match root, if supplied params = root.generateLayoutParams(attrs); if (!attachToRoot) { // Set the layout params for temp if we are not // attaching. (If we are, we use addView, below) temp.setLayoutParams(params); } } ... }
如果parent傳進去為null,生成的View的LayoutParams為null,在RecyclerView.addView時,發現LayoutParams為null,則生成預設的LayoutParams,
// android.view.ViewGroup
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
所以無論無論你怎麼寫,最外層的LinearLayout寬度為WRAP_CONTENT,如果那三個點的寬度為6dp,那麼整個View的寬度也為6dp,所以無法居中。
所以要解決照這個問題需要在inflate的時候將parent傳進去,如:
result = new DividerHolder(mInflater.inflate(R.layout.divider, parent, false));
同時,最後一個引數設定成false,如果不填該引數則拋異常,說先要removeAllViews()
衍生1,為何ListView加進去就是MATCH_PARENT的?
因為AbsListView重寫的generateDefaultLayoutParams方法為
// android.widget.AbsListView
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0);
}
衍生2,為何高度只能用minHeight控制?
同理,layout.xml根節點的attrs屬性沒被寫到LayoutParams中!所以使用minHeight來控制高度的做法是可笑的!你所要做的是在inflate時把parent傳進去!
RecyclerView自定義LayoutManager,打造不規則佈局
http://blog.csdn.net/qibin0506/article/details/52676670