안드로이드 RecyclerView의 ViewHolder의 width가 match_parent안될 때

프로그래밍/Android 관련2018. 3. 29. 19:42

안녕하세요. 개발자 드리머즈입니다.


안드로이드 공부중에 발생한 이슈 정리합니다.


문제 현상

리싸이클러뷰의 행(Row, ViewHolder)의 폭(width)을 가득 채우기위해 match_parent로 설정했음에도 위 그림과 같이 wrap_content처럼 보였습니다.


문제 원인

정확한 원인은 모르겠으나 안드로이드 스튜디오에서 새 프로젝트를 만들면 기본적으로 사용하는 ConstraintLayout과 관련이 있는 것 같습니다.


*activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    >
 
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
</android.support.constraint.ConstraintLayout>
cs



*정확한 문제 원인은 파악중



해결 방법

아래 코드와 같이 RecyclerView를 감싸고 있던 ConstraintLayout을 LinearLayout으로 변경하면 됩니다.


*activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
 
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
 
</LinearLayout>
cs


정상동작하는 사진입니다.

작성자

Posted by 드리머즈

관련 글

댓글 영역