티스토리 뷰
[Android studio] 나만의 커스텀뷰 만들기 코틀린 Create CustomView Kotlin
1. res -> values -> atts.xml 생성 후 속성 데이터 작성
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomViewData">
<attr name="itemTitleText" format="reference|string" />
<attr name="itemContentText" format="reference|string" />
<attr name="itemTitleTextColor" format="reference|color" />
<attr name="itemContentTextColor" format="reference|color" />
</declare-styleable>
</resources>
2. res -> layout -> layout_customview.xml 커스텀뷰 레이아웃 작성
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/space"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Space
android:id="@+id/space"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right|center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/space"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3. CustomView 클래스 작성
open class CustomView(context: Context, attrs: AttributeSet) : ConstraintLayout(context, attrs){
private var textTitle: TextView
private var textContent: TextView
init {
val v = View.inflate(context, R.layout.layout_customview, this)
textTitle = v.findViewById(R.id.title)
textContent = v.findViewById(R.id.content)
context.theme.obtainStyledAttributes(
attrs,
R.styleable.settingItemViewData,
0, 0).apply {
try {
setTitleText(getString(R.styleable.customViewData_itemTitleText))
setContentText(getString(R.styleable.customViewData_itemContentText))
val typeArray = context.obtainStyledAttributes(attrs, R.styleable.settingItemViewData)
setTitleTextColor(typeArray.getColor(R.styleable.customViewData_itemTitleTextColor, 0))
setContentTextColor(typeArray.getColor(R.styleable.customViewData_itemContentTextColor, 0))
} finally {
recycle()
}
}
}
fun setTitleText(text: String?){
mLayout_Setingitemview_Textview_Title.text = text
onRefresh()
}
fun setContentText(text: String?){
mLayout_Setingitemview_Textview_Content.text = text
onRefresh()
}
fun setTitleTextColor(color: Int){
mLayout_Setingitemview_Textview_Title.setTextColor(color)
onRefresh()
}
fun setContentTextColor(color: Int){
mLayout_Setingitemview_Textview_Content.setTextColor(color)
onRefresh()
}
private fun onRefresh(){
invalidate()
requestLayout()
}
}
4. CustomView 사용하기
<com.example.myapp.CustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemTitleText="title"
app:itemContentText="content"
app:itemTitleTextColor="#00ff00"
app:itemContentTextColor="#0000ff"/>
'안드로이드 > 코틀린' 카테고리의 다른 글
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- non-zero exit code detected
- 안드로이드 해시키
- nextjs ssr deploy
- 안드로이드 스튜디오 해시키
- react.js useEffect
- room error
- next.js useEffect
- 커밋 이메일 변경
- 카카오 해시키
- 라이브러리 라이센스
- 깃랩에서 깃허브로
- 코틀린
- 깃허브 잔디 옮기기
- next.js ssr deploy
- react native 오픈소스 라이선스
- Build failed because of webpack errors
- 안드로이드 스튜디오
- github mirror
- react native oss
- amplify next.js
- amplify build error
- 깃허브에서 깃랩으로
- rn 오픈소스 라이센스
- kotlin
- rn oss
- next useEffect
- 깃랩 잔디 옮기기
- Android Studio
- kakao api notworking
- gitlab mirror
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
글 보관함