이번 시간에는 코드(MainActivity)에서 텍스트뷰 색상을 다양하게 변경하는 방법을 알아보겠습니다.
- 설명 -
1. 코드에서 색상변경 할 TextView
2. 코드에서 색상변경 기능 Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/colorTextView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:text="색상 바꾸기"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/changeBtn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="changeColor"
android:text="변경1"
android:textSize="20sp" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/changeBtn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="changeColor"
android:text="변경2"
android:textSize="20sp" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/changeBtn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="changeColor"
android:text="변경3"
android:textSize="20sp" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/changeBtn4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="changeColor"
android:text="변경4"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
- 설명 -
1. fun changeColor(view: View)
색상 변경 함수
2. setBackgroundColor
배경 색상 변경하는 함수
2. ContextCompat.getColor(applicationContext,
R.color.purple_200)
color.xml에서 가져오기
3. Color.rgb(100, 200, 125)
rgb 색상값(red, green, blue)으로 색상 변경
0 ~ 255 값을 사용할 수 있음
4. Color.parseColor("#E4F7BA")
rgb 코드값으로 색상 변경
5. Color.RED
자체 색상으로 색상 변경
class MainActivity : AppCompatActivity() {
lateinit var colorTextView: View
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
colorTextView = findViewById(R.id.colorTextView)
}//onCreate()
/**
* 색상 변경하는 함수
*/
fun changeColor(view: View) {
//조건(버튼 아이디에 따른 색상 변경
when(view.id){
R.id.changeBtn1 ->{
//color.xml에서 가져오기
colorTextView.setBackgroundColor(ContextCompat.getColor(applicationContext,
R.color.purple_200))
}
R.id.changeBtn2 ->{
//rgb 색상값으로 적용( rgb(빨강, 파랑, 초록) 0 ~ 255
colorTextView.setBackgroundColor(Color.rgb(100, 200, 125))
}
R.id.changeBtn3 ->{
//rgb 코드값 적용
colorTextView.setBackgroundColor(Color.parseColor("#E4F7BA"))
}
R.id.changeBtn4 ->{
//자체 색상 적용
colorTextView.setBackgroundColor(Color.RED)
}
}
}
}
2022.09.19 - [안드로이드] - [안드로이드] RecyclerView 홀수 행, 짝수 행 별 색상 다르게 하는 방법
2022.09.13 - [안드로이드] - [안드로이드 코틀린] 액티비티(Activity) 뒤로 가기 버튼 만드는 방법
2022.09.06 - [안드로이드] - [안드로이드 코틀린] 애니메이션 적용해서 배터리 충전하는 방법
[안드로이드 코틀린] RecyclerView 다중 선택 색상 변경하는 방법 (2) | 2022.09.22 |
---|---|
[안드로이드 코틀린] SharedPreferences 간단한 데이터 저장하는 방법 (0) | 2022.09.21 |
[안드로이드 코틀린] RecyclerView 홀수 행, 짝수 행 별 색상 다르게 하는 방법 (0) | 2022.09.19 |
[안드로이드 코틀린] SQLite ToDo List 만드는 방법 part3 - 메인 화면과 메인 코드 (2) | 2022.09.16 |
[안드로이드 코틀린] SQLite ToDo List 만드는 방법 part2 - 할 일 화면과 어댑터 클래스 (0) | 2022.09.15 |
댓글 영역