android item list居中,Android应用开发之RecyclerView 中的 item 如何居中问题

本文将带你了解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频道!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值