【问题标题】:Android Grid Layout Manager not centering elementsAndroid Grid Layout Manager 不居中元素
【发布时间】:2020-11-30 22:14:34
【问题描述】:

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

<?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>

这是我初始化 Recycler View 的方法:

  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 列居中以具有相同的开始和结束间距?

【问题讨论】:

  • 你能发布你的项目布局吗?
  • @McSlinPlay 添加

标签: android android-recyclerview android-gridlayout gridlayoutmanager


【解决方案1】:

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

【讨论】:

  • 要添加到这个答案,如果您有水平对齐问题,您还需要将以下两个属性设置为包装linearlayout。 1)android:layout_width="match_parent" 2)android:gravity="center_horizontal"
猜你喜欢
  • 2021-03-29
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多