본문 바로가기
Android/Code Piece

Stroke의 색상을 동적으로 바꾸기

by featherwing 2020. 4. 23.
반응형

안드로이드의 drawable에 다음과 같이 사용자 정의 drawable resource를 만들 수 있습니다.

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff"/>

    <stroke android:width="3dp"
        android:color="#6aa5ff"/>

    <corners
        android:radius="10dp"/>
</shape>

 

해당 drawable resource를 round_box.xml로 정의하고 backgound로 설정해주면 아래와 같이 나타납니다.

 

<RelativeLayout
            android:id="@+id/search_bar"
            android:background="@drawable/round_box"
            android:padding="8dp"
            android:layout_height="40dp">
            </RelativeLayout>

 

아래와 같은 코드를 사용하시면 코드상에서 동적으로 stroke의 색상을 변경할 수 있습니다.

RelativeLayout searchBackground = mainView.findViewById(R.id.search_bar);
GradientDrawable mGradientDrawable = (GradientDrawable) searchBackground.getBackground();
mGradientDrawable.setStroke(10, Color.RED);

 

자세한 내용은 안드로이드 개발자 문서의 내용을  참고 해 주세요. 

반응형

댓글