본문 바로가기

Android

[Android,Java] 리스트뷰 배경 테두리(Border) 및 라운드 넣기

라운드된 배경을 xml로 만들어서 사용하면 거진 바로 적용이되는데

리스트뷰는 이상하게 라운드된 뷰 위로 내용들이 삐져나와서 제대로된 화면이 나오질 않았다

clip옵션도 줘보고 백그라운드도 여기저기 옴겨가면서 해봤는데 안되서 

최종적으로 커스텀뷰와 백그라운드 두가지를 설정하니 정상적으로 동작하였다.

 

해결!!

 

리스트 뷰 백그라운드에 사용할 XML을 먼저 작성한다

 

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <stroke android:width = "1dp" android:color = "#000000"/>
            <corners
                android:radius="5dp"/>
        </shape>
    </item>

    <item
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp">
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF"/>
            <corners
                android:radius="5dp"/>
        </shape>
    </item>
</layer-list>

설명은 앞단 포스트 참조 

 

보통은 해당 xml을 백그라운드로 지정하면 된다고 하지만 

어째서인지 계속 먹히지 않고 배경은 라운드지만 리스트 아이템들이 삐져나와서 사각형이 보이는 형태가 되어버렸다

 

SwipeRefreshLayout 을 사용해서인지 RecyclerView 를 사용해서인지도 모르겠다

그래서 이래저래 다 해보다가 최종적으로는 커스텀뷰로 RecyclerView를 만들고

SwipeRefreshLayout 백그라운드에 라운드된 xml을 연결

 

public class RoundedRecyclerView extends RecyclerView {
    private Path path;
    public RoundedRecyclerView(@NonNull Context context) {
        super(context);
    }

    public RoundedRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RoundedRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        if (path == null) {
            path = new Path();
            path.addRoundRect(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), 20, 20, Path.Direction.CW);
        }
        canvas.clipPath(path);
        super.dispatchDraw(canvas);
    }
}

RoundedRecyclerView 라는 Class를 만들고 RecyclerView를 상속받는다

그리고 dispatchDraw 를 오버라이드해서 라운드되게 path 를 설정해서 그리게 한다 

 

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:layout_marginTop="15dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:id="@+id/swipe_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="10dp"
    android:background="@drawable/border_round5_white_background"
    android:layout_weight="1">

    <패키지명.RoundedRecyclerView
        android:padding="1dp"
        android:id="@+id/attendance_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

 

 

위와같이 설정을 하고 커스텀뷰로 작성을하니 정상적으로 리스트뷰 외각에 라운드와 테두리가 들어간 배경안으로

내용들이 잘 clip 되었다