Android Grid Layout Manager不在元素居中

问题描述

我在约束布局中有以下回收者视图(非常简单)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activities.PokemonFavoritesActivity"
tools:showIn="@layout/activity_pokemon_favorites">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv_pokemon_favorites"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="2dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

这是我初始化回收站视图的方法:

  private void initializeRecyclerViewComponents() {
    favoritesRecyclerView = findViewById(R.id.rv_pokemon_favorites);
    layoutManager = new GridLayoutManager(this,4);
    favoritesRecyclerView.setLayoutManager(layoutManager);
    pokemonFavoritesAdapter = new PokemonFavoritesAdapter(this,favoritePokemon);
    pokemonFavoritesAdapter.setClickListener(this);
    favoritesRecyclerView.setAdapter(pokemonFavoritesAdapter);
}

这是item_layout:

<?xml version="1.0" encoding="utf-8"?>
<com.mikhaellopez.circularimageview.CircularImageView 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/civ_pokemon_favorite"
 android:layout_width="100dp"
 android:layout_height="100dp" />

但是,如您在图像中看到的,视图不是“居中的”(与开始处相比,每行的末尾有很多多余的边距) 如何使4列居中以具有相同的开始和结束间距?

解决方法

尝试将您的项目视图包装在线性布局中,并在其中居中放置图像。那应该解决它。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...