Android Application
Kotlin 메뉴(Menu) : 컨텍스트 메뉴(ContextMenu)- 배경색 변경
작성자 임베디드코리아
작성일23-11-16 11:50
조회1,591회
댓글0건
Kotlin 메뉴(Menu) : 컨텍스트 메뉴(ContextMenu)
- 배경색 변경 및 버튼 변경 앱 만들기
-------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/baseLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="배경색 변경" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="버튼 변경" />
</LinearLayout>
------------------------------------------------------------------------------------
MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
lateinit var button1 : Button
lateinit var button2 : Button
lateinit var baseLayout : LinearLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "배경색 바꾸기(Kotlin 코드)"
baseLayout = findViewById<LinearLayout>(R.id.baseLayout) as LinearLayout
button1 = findViewById<View>(R.id.button1) as Button
registerForContextMenu(button1)
button2 = findViewById<View>(R.id.button2) as Button
registerForContextMenu(button2)
}
override fun onCreateContextMenu(menu: ContextMenu, v: View,
menuInfo: ContextMenu.ContextMenuInfo?) {
super.onCreateContextMenu(menu, v, menuInfo)
if (v === button1) {
menu.setHeaderTitle("배경색 변경")
menu.add(0, 1, 0, "배경색 (빨강)")
menu.add(0, 2, 0, "배경색 (초록)")
menu.add(0, 3, 0, "배경색 (파랑)")
}
if (v === button2) {
menu.add(0, 4, 0, "버튼 45도 회전")
menu.add(0, 5, 0, "버튼 2배 확대")
}
}
override fun onContextItemSelected(item: MenuItem) : Boolean {
when (item.itemId) {
1 -> {
baseLayout.setBackgroundColor(Color.RED)
return true
}
2 -> {
baseLayout.setBackgroundColor(Color.GREEN)
return true
}
3 -> {
baseLayout.setBackgroundColor(Color.BLUE)
return true
}
4 -> {
button2.rotation = 45f
return true
}
5 -> {
button2.scaleX = 2f
return true
}
}
return false
}
}
-------------------------------------------------------------------------------------
***** themes.xml **********
-------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.ContextMenuColor" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="colorPrimary">#E91E63</item>
<item name="colorPrimaryDark">#3F51B5</item>
</style>
<style name="Theme.ContextMenuColor" parent="Base.Theme.ContextMenuColor" />
</resources>