본문 바로가기

IT/안드로이드 관련

[안드로이드] 둥근 모서리 레이아웃 child view

안녕하세요 남갯입니다.


오늘은 둥근모서리 안에 child view도 둥근모양을 적용하는 방법에 대해 알려드리려고합니다.



<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

<corners android:radius="100dip"/>

<stroke
android:width="1dp"
android:color="#000000">
</stroke>

</shape>



우리는 이런모양을 통해 circle한 모양을 만들었습니다.


예를들어 이 드로어블을 적용해서 레이아웃을 만들었습니다.


<LinearLayout
android:id="@+id/myLinearLayout"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:orientation="horizontal"
android:background="@drawable/shape_stroke_layout"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="40dp"/>


이렇게 말이죠!




하지만 여기다가 네모난 뷰를 넣게되면?







이렇게 네모난 child view가 덮어버리게 됩니다.



api 21부터는 cliptoOutline 이라는 기능을 제공합니다.



따라서



val linearLayout = myLinearLayout.apply {
measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED)
clipToOutline= true
}


여기서 true로 설정하면 







이렇게 바뀌는것을 볼 수있습니다.