Android Application
Kotlin 메뉴(Menu) : 앱바(App Bar)에 액션(Action)
작성자 임베디드코리아
작성일23-09-25 00:44
조회1,720회
댓글0건
------------------------------------------------------------------------------------
activity_main.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="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/TextMenuItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option Menu Example"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
------------------------------------------------------------------------------------
*** values/strings.xml
------------------------------------------------------------------------------------
<resources>
<string name="app_name">ActionBarKotlin</string>
</resources>
------------------------------------------------------------------------------------
*** menu/actionbar_main.xml
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/item01"
app:showAsAction="ifRoom"
android:icon="@android:drawable/ic_menu_search"
android:title="Item 1" />
<item
android:id="@+id/item02"
app:showAsAction="ifRoom"
android:icon="@android:drawable/ic_menu_share"
android:title="Item 2" />
<item
android:id="@+id/item03"
app:showAsAction="ifRoom"
android:title="Item 3">
<menu>
<item
android:id="@+id/item3_1"
app:showAsAction="ifRoom"
android:title="Item 3-1" />
<item
android:id="@+id/item3_2"
app:showAsAction="ifRoom"
android:title="Item 3-2" />
</menu>
</item>
<item
android:id="@+id/item04"
app:showAsAction="ifRoom"
android:title="Item 4" />
</menu>
------------------------------------------------------------------------------------
*** values/themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.ActionBarKotlin" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Customize your light theme here. -->
<item name="colorPrimary">#3949AB</item>
<item name="colorPrimaryDark">#4682B4</item>
<item name="colorAccent">#FFC940</item>
<item name="android:textColor">@color/black</item>
<item name="android:textColorPrimary">#F4511E</item>
<item name="android:textColorPrimaryInverse">#F4511E</item>
<item name="android:windowBackground">#C0CA33</item>
</style>
<style name="Theme.ActionBarKotlin" parent="Base.Theme.ActionBarKotlin" />
</resources>
------------------------------------------------------------------------------------
MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity(), PopupMenu.OnMenuItemClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_main)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btnMenu1.setOnClickListener { showPopup(binding.btnMenu1) }
}
// 팝업 메뉴 보여주는 커스텀 메소드
private fun showPopup(v: View) {
val popup = PopupMenu(this, v) // PopupMenu 객체 선언
popup.menuInflater.inflate(R.menu.popup_menu, popup.menu) // 메뉴 레이아웃 inflate
popup.setOnMenuItemClickListener(this) // 메뉴 아이템 클릭 리스너 달아주기
popup.show() // 팝업 보여주기
}
// 팝업 메뉴 아이템 클릭 시 실행되는 메소드
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) { // 메뉴 아이템에 따라 동작 다르게 하기
R.id.PopUP01 -> Toast.makeText(this, "첫번째 팝업 메뉴 클릭", Toast.LENGTH_LONG).show()
R.id.PopUP02-> Toast.makeText(this, "두번째 팝업 메뉴 클릭", Toast.LENGTH_LONG).show()
R.id.PopUP03 -> Toast.makeText(this, item.title, Toast.LENGTH_LONG).show()
}
return item != null // 아이템이 null이 아닌 경우 true, null인 경우 false 리턴
}
}