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 | 29 |
30 | 31 |
Tags
- TypeScript
- React
- 코틀린
- Linux
- fragment
- Java
- 자바스크립트
- Kotlin
- TextView
- 오버라이딩
- button
- Hook
- npm
- JS
- 구글맵스
- JavaScript
- GoogleMaps
- nodejs
- array
- 랜덤넘버
- 리액트
- 스프링부트
- stylesheet
- Javscript
- Android
- scrollview
- 랜덤번호
- 안드로이드
- RecyclerView
- SpringBoot
Archives
- Today
- Total
타닥타닥 개발자의 일상
Kotlin으로 메뉴 만들고 메뉴 이용해 TextView 글자색 변경, 글자 배경 변경하기 본문
파일 생성
res/menu 폴더에 context_menu_main.xml 파일 생성
context_menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/text_color"
android:title="글자색 변경"/>
<item
android:id="@+id/text_back_color"
android:title="배경색 변경" />
<item
android:id="@+id/text_basic"
android:title="초기화"/>
</menu>
메뉴 구성하는 항목 만들기
build.gradle(:app)
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.sample13"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures{
viewBinding true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
기존 코드에서
buildFeatures{
viewBinding true
}
부분 추가되었다.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ContextMenu 보기"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main에서는 메뉴에 대한 내용이 존재하지 않는다.
MainActivity.kt
package com.example.sample13
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ContextMenu
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import com.example.sample13.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
val binding by lazy { ActivityMainBinding.inflate(layoutInflater)}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_main) 아래의 binding.root 이용하므로 주석처리
setContentView(binding.root)
registerForContextMenu(binding.textView)
//메뉴를 activity메인에 있는 textView에 넣는다. 화면상 "ContextMenu보기" 를 꾹 누르면 메뉴가 뜬다.
}
override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
menuInflater.inflate(R.menu.context_menu_main, menu)
//R은 res 폴더의 약자. res폴더 안에 있는 context_menu_main.xml 파일과 연결시킨다.
}
override fun onContextItemSelected(item: MenuItem): Boolean {
val textView = findViewById<TextView>(R.id.textView)
//item id가 무엇이었을때?
when(item?.itemId){
R.id.text_color -> {
textView.text = "글자색 변경"
textView.setTextColor(Color.CYAN)
//메뉴중 text_color라는 항목을 선택했을시, 화면의 textView를 "글자색 변경" 이라표시
//글자색은 Cyan색으로 변경
}
R.id.text_back_color-> {
textView.text = "배경색 변경"
textView.setBackgroundColor(Color.DKGRAY) //진한 회새개으로 글자 배경색 변경
}
R.id.text_basic -> {
textView.text= "초기화"
}
}
return super.onContextItemSelected(item)
}
}
결과화면
Comments