如何将回收视图的项目居中?

问题描述 投票:0回答:11

我需要将每行中的元素居中,这样它们就会像在这个模型中一样。 我一直在寻找是否有任何布局可以在不看的情况下以这种方式工作。 项目在其行中居中。

mockup

这就是我的代码中现在的样子。enter image description here

android android-layout android-recyclerview
11个回答
83
投票

将 recyclerview

width
更改为
wrap_content
及其容器的
layout_gravity
更改为
center_horizontal

 <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">

      <android.support.v7.widget.RecyclerView
           android:id="@+id/recycrer_view"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center_horizontal"
           android:paddingLeft="16dp"
           android:paddingRight="16dp" />
 </LinearLayout>

41
投票

LinearLayout
放入
RecyclerView
的项目行布局中,然后将
android:layout_gravity="center"
赋予
LinearLayout

对于每一行图像,您必须拍摄不同的

LinearLayout

这是示例代码:

 <LinearLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:orientation="horizontal">

            <ImageView
                android:id="@+id/image1"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:src="@drawable/image1" />

            <ImageView
                android:id="@+id/image2"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:src="@drawable/image2" />

           <ImageView
                android:id="@+id/image3"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:src="@drawable/image3" />

  </LinearLayout>

37
投票

实现:

recyclerView.adapter = MyAdapter()
val layoutManager = FlexboxLayoutManager(context).apply {
    justifyContent = JustifyContent.CENTER
    alignItems = AlignItems.CENTER
    flexDirection = FlexDirection.ROW 
    flexWrap = FlexWrap.WRAP
}
recyclerView.layoutManager = layoutManager

您需要将 FlexboxLayout 添加到您的 gradle 中:

implementation 'com.google.android.flexbox:flexbox:3.0.0'

enter image description here


8
投票

我假设您使用

LinearLayoutManager
RecyclerView
来实现
ListView
风格的效果。在这种情况下,请对每行使用
horizontal
LinearLayout
,并使用
android:gravity="center"
将其内容居中。


6
投票

使用

FlexboxLayout
库中的
com.google.android:flexbox
。请注意
flexwrap
justifyContent
属性值。然后,在要换行之前的项目行的视图上设置
layout_wrapBefore = true

    <com.google.android.flexbox.FlexboxLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:flexWrap="wrap"
        app:justifyContent="center">

        <View ... />

        <View app:layout_wrapBefore="true" ... />

        <View ... />

    </com.google.android.flexbox.FlexboxLayout>

4
投票

使用

gridLayoutManager = new GridLayoutManager(context,6)
并覆盖
setSpanSizeLookup

示例:

int totalSize=list.size();
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        int span;
                        span = totalSize % 3;
                        if (totalSize < 3) {
                            return 6;
                        } else if (span == 0 || (position <= ((totalSize - 1) - span))) {
                            return 2;
                        } else if (span == 1) {
                            return 6;
                        } else {
                            return 3;
                        }
                    }
                });

如果您想连续显示 3 个项目并保持在网格的中心,这将起作用。

根据您的要求更改网格布局管理器跨度计数。


4
投票

您可以使用谷歌的新布局管理器FlexLayoutManager 它将为您提供对 recyclerView 项目的大量控制和灵活性


3
投票

您可以动态添加一些布局,例如:

  • 适配器对象可以是水平LinearLayout

1-在java代码中创建一个LinearLayout并自定义它(重力,...)

2-向 LinearLayout 添加一些图标

3-将 LinearLayout 添加到您的适配器

4- 重复 1,2,3


    // : declare new horizontal linearLayout
    ImageView myIcons[nomOfIcons];
    // : add all icons to myIcons
    for(int i=1; i<=nomOfIcons; i++){
        linearLayout.addView(myIcons[i - 1]);
        if(i%numOfIconsInOneHorizontalLinearLayout==0) {
            results.add(linearLayout); // add linearLayout to adapter dataSet
            // : declare new horizontal linearLayout
        }
    }
    if(n%numOfIconsInOneHorizontalLinearLayout!=0) // add last linearLayout if not added in the loop
        results.add(linearLayout);
    mAdapter.notifyDataSetChanged(); // update adapter


2
投票

相信我,这会起作用的。 在放置回收器视图的主 xml 文件中,复制我的代码。

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/_50sdp"
        android:gravity="center">

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="wrap_content"
            android:layout_height="@dimen/_50sdp"/>

    </LinearLayout>

1
投票

目前,我认为我们需要为其编写自己的自定义视图,因为 Android 尚未像我们预期的那样支持此功能。

这是我制作的示例,以防有人需要: https://github.com/mttdat/utils

(尝试通过调整Manifest文件来尝试CenterGridActivity.kt以默认启动此活动)


0
投票

如果您需要AskQ回复的Java版本:

    // Set layout manager to position the items
    FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
    layoutManager.setJustifyContent(JustifyContent.CENTER);
    layoutManager.setAlignItems(AlignItems.CENTER);
    layoutManager.setFlexDirection(FlexDirection.ROW);
    layoutManager.setFlexWrap(FlexWrap.WRAP);
    adapter.setLayoutManager(layoutManager);
© www.soinside.com 2019 - 2024. All rights reserved.