【问题标题】:Centralizing RecyclerView with LinearLayoutManager使用 LinearLayoutManager 集中 RecyclerView
【发布时间】:2017-06-04 21:52:26
【问题描述】:

我正在尝试将RecyclerView 列表集中在屏幕上。这将是一个单一的列表,所以我将使用LinearLayoutManager (LLM)。 然而,使用 LLM 它不会集中项目,它在横向模式下看起来像这样:

XML 代码如下所示:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"

    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/recipe_card"
        android:layout_gravity="center"
        android:layout_width="400dp"
        android:layout_height="186dp"
        style="@style/CardViewStyle">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <ImageView
                android:id="@+id/recipe_image"
                android:layout_width="120dp"
                android:layout_height="163dp"
                app:srcCompat="@mipmap/ic_pan"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_marginTop="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                app:layout_constraintLeft_toLeftOf="parent"
                tools:layout_editor_absoluteY="3dp" />

            <TextView
                android:id="@+id/recipe_name"
                android:layout_width="250dp"
                android:layout_height="119dp"
                android:text="TextView"
                app:layout_constraintLeft_toRightOf="@+id/recipe_image"
                android:layout_marginLeft="8dp"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_marginTop="16dp" />

            <TextView
                android:id="@+id/recipe_servings"
                android:layout_width="164dp"
                android:layout_height="32dp"
                android:text="TextView"
                android:layout_marginTop="8dp"
                app:layout_constraintTop_toBottomOf="@+id/recipe_name"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_marginBottom="8dp"
                app:layout_constraintLeft_toRightOf="@+id/recipe_image"
                android:layout_marginLeft="94dp"
                app:layout_constraintVertical_bias="0.0" />
        </android.support.constraint.ConstraintLayout>

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

</FrameLayout>

在 Android Studio 的 Preview 上,它看起来不错,但在应用程序上,它却不行。 我设法通过将 LLM 更改为只有一列的 GridLayoutManager 来解决它。但它看起来很丑。

有谁知道发生了什么? 谢谢。

【问题讨论】:

  • 大卫检查我的答案
  • 大卫,我的回答可以极大地帮助你,你检查过了吗?

标签: android android-recyclerview linearlayoutmanager


【解决方案1】:

尝试将主布局的内容重心设置为中心。有两种方法可以做到这一点:

  1. 在框架布局中添加“centerHorozontal”属性为“true”。
  2. 将框架布局更改为线性布局,并将主线性布局的重心设置为“中心”。

希望它会起作用,如果不起作用,那么只需将您在布局中使用的图像发送给我,我会尝试使用其他一些技术来修复它,并将更新的代码发送给您。

【讨论】:

    【解决方案2】:

    将约束布局替换为相对布局

    然后,使图像居中,使用这个

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:clickable="true"
            android:scaleType="centerCrop" />
    

    然后也使这些文本中的每一个都成为中心

    android:textAlignment="center"
    

    【讨论】:

      【解决方案3】:

      请检查您的public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType),如果您这样写:

      @Override
      public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
              return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, null));
      }
      

      这将使您的 FrameLayout 被禁用,而 CardView 将成为您的 ViewHolder 的 itemView,因此您应该进行如下更改: @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, parent, false)); } 希望能帮到你。

      【讨论】:

        【解决方案4】:

        如果我是你,我会得到我的显示尺寸宽度并相应地设置我的ViewHolder 一些边距。

        这是我的应用程序类中的代码:

        public class MyApp extends Application {
        public static Point displaySize = new Point();
        public static float density = 1;
        public static Context appContext = null;
        public static int height;
        
        @Override
        public void onCreate() {
            super.onCreate();
            ActiveAndroid.initialize(this);
            appContext = getApplicationContext();
            checkDisplaySize();
            density = appContext.getResources().getDisplayMetrics().density;
        }
        
        public static void checkDisplaySize() {
            try {
                WindowManager manager = (WindowManager)  
        appContext.getSystemService(Context.WINDOW_SERVICE);
                if (manager != null) {
                    Display display = manager.getDefaultDisplay();
                    if (display != null) {
                            display.getSize(displaySize);
                    }
                   height = (int) (displaySize.x / 1.777f); //Aspect Ratio 16:9
                }
            } catch (Exception e) {
            }
        }
        }
        

        在您获得持有人视图的适配器中完成所有这些之后,只需执行以下操作:

        int marginWidth = (MyApp.desplaySize.x - itemView.getMeasuredWidth())/2;
        LayoutParams params = new LayoutParams(
            LayoutParams.WRAP_CONTENT,      
            LayoutParams.WRAP_CONTENT
        );
        params.setMargins(marginWidth, 0, marginWidth, 0);
        itemView.setLayoutParams(params);
        

        我不确定这个确切的代码是否能解决您的问题,但您可以轻松地更改它,这样就可以了。

        【讨论】:

          【解决方案5】:

          在您的 FrameLayout 中编写此代码

              android:layout_gravity="center"
          

          【讨论】:

            猜你喜欢
            • 2017-01-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-08-14
            相关资源
            最近更新 更多