Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 운영체제 멀티 프로그래밍
- 운영체제 멀티 태스킹
- 개발 포지션 변경
- IT 포지션 변경
- 운영체제 작동방식
- spring responseEntity response cause
- spring paging sort sql injection
- spring 동적 쿼리 주의사항
- spring dynamic query sql injection
- 운영체제 자원관리
- spring sql injection 방지
- android 타이머
- OS 자원관리
- 백엔드 직무 변경
- 운영체제 다중모드
- spring exception stackTrace remove
- Android Timer
- spring responseEntity response stackTrace
- aws ec2 scp 파일 전송
- 개발 직무 변경
- 운영체제 공룡책
- spring exception cause remove
- spring exceptionHandler reposnse stackTrace
- IT 직무 변경
- 백엔드 포지션 변경
- spring exceptionHandler response cause
- ec2 scp 파일 전송
- 운영체제 커널모드
- 운영체제 개념
- spring sql injection
Archives
- Today
- Total
오늘도 삽질중
Android CircleTextView 커스텀으로 구현하기 본문
반응형
텍스트뷰나 또는 다른 뷰에 동그랗게 라운드 효과를 주기 위해서 조금만 구글링을 해보면
여러가지 자료가 많이 나온다.
대표적으로 아래 코드처럼 Shape Xml 파일을 만들어서 직접 레이아웃에 세팅하는걸 많이 봤을것이다.
circle_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/black"/>
<corners android:radius="4dp"/>
<size android:width="24dp"
android:height="24dp"/>
</shape>
근데 drawable의 백그라운드 컬러를 바꿔야 하는 상황이 온다면?
물론 코드로 작업이 가능하지만 아예 커스텀뷰로 빼서 쉽게 쓰는 방법을 생각해봤다.
커스텀뷰를 만드는 기본방법을 안다는 전제하에 상세한 설명은 생략합니다.
CircleTextView.kt
class CircleTextView @JvmOverloads constructor(
context: Context,
attr: AttributeSet
): AppCompatTextView(context,attr,0) {
private var circleBackgroundColor: Int = Color.TRANSPARENT
init {
val typeArray = context.obtainStyledAttributes(attr , R.styleable.CircleTextView , 0 , 0)
circleBackgroundColor = typeArray.getColor(R.styleable.CircleTextView_circleBackgroundColor , Color.TRANSPARENT)
typeArray.recycle()
setBackgroundResource(R.drawable.circle_bg)
if (circleBackgroundColor != Color.TRANSPARENT){
setDrawableBackgroundColor(circleBackgroundColor)
}
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
}
fun setDrawableBackgroundColor(color: Int) {
circleBackgroundColor = color
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
background.colorFilter = BlendModeColorFilter(circleBackgroundColor, BlendMode.SRC_IN)
}else {
background.setColorFilter(circleBackgroundColor, PorterDuff.Mode.SRC_IN)
}
}
}
attr.xml
<declare-styleable name="CircleTextView">
<attr name="circleBackgroundColor" format="color"/>
</declare-styleable>
만들고 나면 Xml 에서 이렇게 사용이 가능하다
activity_test.xml
<test.samplecoordinatorlayout.widgets.CircleTextView
android:id="@+id/circle_text"
android:layout_width="24dp"
android:layout_height="24dp"
android:text="1"
android:textColor="@color/white"
android:gravity="center"
android:textSize="12sp"
/>
<test.samplecoordinatorlayout.widgets.CircleTextView
android:id="@+id/circle_text2"
android:layout_width="24dp"
android:layout_height="24dp"
android:text="2"
android:textColor="@color/white"
android:gravity="center"
android:textSize="12sp"
app:circleBackgroundColor="@color/teal_700"
/>
<test.samplecoordinatorlayout.widgets.CircleTextView
android:id="@+id/circle_text3"
android:layout_width="24dp"
android:layout_height="24dp"
android:text="3"
android:textColor="@color/white"
android:gravity="center"
android:textSize="12sp"
app:circleBackgroundColor="@color/purple_200"
/>
Drawable 백그라운드 컬러를 변경하고 싶다면 커스텀뷰에 선언한 함수를 이용해 쉽게 체인지가 가능하다.
binding.circleText.setDrawableBackgroundColor(Color.RED)
binding.circleText2.setDrawableBackgroundColor(Color.YELLOW)
binding.circleText3.setDrawableBackgroundColor(Color.BLUE)
반응형
'안드로이드' 카테고리의 다른 글
Android PopupMenu 백그라운드+텍스트 컬러 변경 (0) | 2021.12.18 |
---|---|
Android Rxjava interval 로 타이머 만들어보기 (0) | 2021.10.14 |
Android layer-list 에서 특정 item만 backgroundColor 변경 (0) | 2021.09.29 |
Android NestScrollView in RecyclerView dynamic Bottom Button (0) | 2021.09.07 |
호기심으로 찾아본 카카오톡 채팅이미지 사이즈 변환규칙 (0) | 2021.08.07 |
Comments