반응형

View의 background 속성을 이용해 View의 모양 변경하기


layout xml의 적용 부분

<View
android:id="@+id/marker"
android:layout_width="match_parent"
android:layout_height="7dp"
android:background="@drawable/marker_bound_top" />


marker_bound_top.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle">
<corners android:topLeftRadius="10dp" android:topRightRadius="10dp" />
<solid android:color="@color/default_marker_color"/>
</shape>
</item>
</layer-list>


shape Background 지정한 상태에서 필요에 따라 배경색상 변경

*setBackgroundColor()로 색상을 변경할 경우, xml로 지정한 내용이 무효화 된다.

아래와 같은 코드로 모양은 유지하면서 배경색만 변경 가능하다.

public void setMarkerColor(@ColorInt int color) {

Drawable backgroundOff = vMarker.getBackground();
backgroundOff.setTint(color);
vMarker.setBackground(backgroundOff);

}


반응형

+ Recent posts