GridLayoutManager 不居中对齐问题

Paste_Image.png

蛋疼的问题,右边留空那么多。

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:background="#ff0"
    >

</android.support.v7.widget.RecyclerView>

item项的布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:background="#f0f"
    android:gravity="center"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="52dp"
        android:layout_height="52dp"
        app:srcCompat="@mipmap/home_new"
        android:id="@+id/iv_icon" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textColor="#4A4A4A"
        android:layout_marginTop="10dp"
        android:id="@+id/tv_name" />
</LinearLayout>

设置GridLayoutManager 设置一行显示4个

                GridLayoutManager  gridLayoutManager = new GridLayoutManager(container.getContext(), 4);
                recyclerView.setLayoutManager(gridLayoutManager);//一行显示4个

效果就成上面那样蛋疼了,经分析后,发现调整下item的根LinearLayout 的宽属性即可

android:layout_width="match_parent"
Paste_Image.png

效果如下

Paste_Image.png

若需要间距,就配置一下ItemDecoration


Paste_Image.png
    class MarginDecoration extends RecyclerView.ItemDecoration {
        private int margin;

        public MarginDecoration(Context context) {
            margin = PxUtils.dpToPx(10,context);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.set(margin, margin, margin, margin);
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,136评论 25 709
  • Tangram是阿里出品、用于快速实现组合布局的框架模型,在手机天猫Android&iOS版 内广泛使用 该框架提...
    wintersweett阅读 8,729评论 0 1
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,871评论 0 17
  • 我不属于能把自己爱好做到极致的人,但我依然能感受到在做喜欢事情时,想不断尝试新可能并在过程中越做越好的冲动喜悦。我...
    眠石阅读 1,652评论 0 1
  • 本篇文章共:968字. 预计阅读时长:2分钟. 咱们别一上来就聊同性,伦家没经验哈。 你们有没有发现,为什么有时...
    Caterina原创阅读 3,153评论 0 1

友情链接更多精彩内容