Android Application
Kotlin 메뉴(Menu) : 옵션 메뉴(option menu)-배경색 변경 앱
작성자 임베디드코리아
작성일 23-11-16 11:46
조회1,373회
댓글0건
Kotlin 메뉴(Menu) : 옵션 메뉴(option menu)
배경색 변경 앱 만들기
-------------------------------------------------------------------------------------
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="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="오른쪽 위 메뉴 버튼을 누르세요"
android:textSize="20dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이건 버튼" />
</LinearLayout>
------------------------------------------------------------------------------------
MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
lateinit var baseLayout : LinearLayout
lateinit var button1 : Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "배경색 바꾸기"
baseLayout = findViewById<LinearLayout>(R.id.baseLayout)
button1 = findViewById<Button>(R.id.button1)
}
override fun onCreateOptionsMenu(menu: Menu?) : Boolean {
super.onCreateOptionsMenu(menu)
var mInflater = menuInflater
mInflater.inflate(R.menu.menu1, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem) : Boolean {
when (item.itemId) {
R.id.itemRed -> {
baseLayout.setBackgroundColor(Color.RED)
return true
}
R.id.itemGreen -> {
baseLayout.setBackgroundColor(Color.GREEN)
return true
}
R.id.itemBlue -> {
baseLayout.setBackgroundColor(Color.BLUE)
return true
}
R.id.subRotate -> {
button1.rotation = 45f
return true
}
R.id.subSize -> {
button1.scaleX = 2f
return true
}
}
return false
}
}
-------------------------------------------------------------------------------------
***** menu1.xml * ******
menu 폴더 만들기 --> New ---> Android Resource Directory
menu.xml 만들기 --> New ---> Menu Resource File
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/itemRed"
android:title="배경색 (빨강)">
</item>
<item
android:id="@+id/itemGreen"
android:title="배경색(초록)">
</item>
<item
android:id="@+id/itemBlue"
android:title="배경색(파랑)">
</item>
<item android:title="버튼 변경 >> ">
<menu>
<item
android:id="@+id/subRotate"
android:title="버튼 45도 회전"/>
<item
android:id="@+id/subSize"
android:title="버튼 2배 확대"/>
</menu>
</item>
</menu>
-------------------------------------------------------------------------------------
***** themes.xml **********
-------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.MenuOptionmenu" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="colorPrimary">#7CB342</item>
<item name="colorPrimaryDark">#00897B</item>
</style>
<style name="Theme.MenuOptionmenu" parent="Base.Theme.MenuOptionmenu" />
</resources>
158-840 서울시 양천구 남부순환로 571(신월동, 영남타운 307호) l 대표: 박길성 ㅣ Tel:02-2695-1114 ㅣ Fax:02-2695-1113
Copyright © 2015 Embedded Korea. All Rights Reserved.