本文将帶你了解Android應用開發之RecyclerView 中的 item 如何居中問題,希望本文對大家學Android有所幫助。
一個很簡單的Item布局,我隻要讓它由上而下排列,文字居中
Xml代碼
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
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"
/>
然後代碼這樣寫:很标準的使用方式吧?
Java代碼 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...); 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)讀進去,代碼如下:
Java代碼 // 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); } } ... } // 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,
Java代碼 // android.view.ViewGroup protected LayoutParams generateDefaultLayoutParams() { return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); } // android.view.ViewGroup
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
是以無論無論你怎麼寫,最外層的LinearLayout寬度為WRAP_CONTENT,如果那三個點的寬度為6dp,那麼整個View的寬度也為6dp,是以無法居中。
是以要解決照這個問題需要在inflate的時候将parent傳進去,如:
Java代碼 result = new DividerHolder(mInflater.inflate(R.layout.divider, parent, false)); result = new DividerHolder(mInflater.inflate(R.layout.divider, parent, false));
同時,最後一個參數設定成false,如果不填該參數則抛異常,說先要removeAllViews()
衍生1,為何ListView加進去就是MATCH_PARENT的?
因為AbsListView重寫的generateDefaultLayoutParams方法為
Java代碼 // android.widget.AbsListView @Override protected ViewGroup.LayoutParams generateDefaultLayoutParams() { return new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0); } // 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,打造不規則布局
本文由職坐标整理并釋出,希望對同學們有所幫助。了解更多詳情請關注職坐标移動開發之Android頻道!