[anroid/kotlin] recyclerview + gridlayout 요소 가운데 정렬하기

recyclerview를 이용해 gridview를 동적으로 생성하였는데

요소들이 왼쪽 정렬이 되었다. 가운데 정렬을 하기 위해 gravity를 center로 하면 해결이 될 줄 알았으나 실패..

 

이것저것 해보다가 결국 찾았다.

width를 match parent로 두어 최대로 하고 

margin을 주어 여백을 만들었다. 

 

그리고 하위 요소에 adjustviewbounts를 true로 주어서 가로 세로 비율을 동일하게 주었다. 

 

코드

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    app:cardCornerRadius="15dp">
    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/image"
        android:adjustViewBounds="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</androidx.cardview.widget.CardView>

 

결과