RecyclerView 中的 item 如何居中问题
来源:互联网 发布:union软件安卓 编辑:程序博客网 时间:2024/04/26 23:41
一个很简单的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.LayoutInflaterpublic 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.ViewGroupprotected 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@Overrideprotected 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
0 0
- RecyclerView 中的 item 如何居中问题
- Android RecyclerView 中的 item 如何居中
- 解决RecyclerView 使用 gridlayoutmanager 时item不居中问题
- 问题:如何让HorizontalScrollView中的item能滚动的时候点击居中
- 删除recyclerview中的item
- RecyclerView居中问题
- 10.RecyclerView中的item点击事件,如何实现
- RecyclerView中的item点击事件
- RecyclerView item刷新动画问题
- databinding listview/RecyclerView的item布局文件中如何加载res中的图片
- NGUI 的ScrollView如何让item居中
- Android RecyclerView item没有match parent问题
- RecyclerView抢夺item焦点的问题
- RecyclerView中item无法充满的问题
- 关于RecyclerView 的Item 自适应问题
- 【RecyclerView】 item不能横向充满的问题
- RecyclerView Item不显示的问题
- RecyclerView的Item充满屏幕的问题
- hive 0.13.0 升级到 2.1.1
- 把任意Drawable转换成基于progress填充的drawable
- 仿微信页面切换图标颜色渐变效果
- canvas实现简单的Amaziograph效果画对称图
- sqlite3:not found 解决方法
- RecyclerView 中的 item 如何居中问题
- C++字符串数字的比较
- android5.0使用Notification报RemoteServiceException的解决办法
- java-基本数据类型 循环
- 可设置圆角背景边框的按钮, 通过调节色彩明度自动计算按下(pressed)状态颜色
- 左中右结构的行布局
- which命令
- PhotoView点击放大图片效果
- MVC、MVP、MVVM的区别