7. Popup Menu
페이지 정보
작성자 관리자 댓글 0건 조회 2,527회 작성일 21-06-08 21:32본문
7. Popup Menu
1. 레이아웃 추가
파일명 : 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">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Popup"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2. res/menu 디렉토리 생성
3. popup_menu.xml 추가
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/one"
android:title="One"/>
<item
android:id="@+id/two"
android:title="Two"/>
<item
android:id="@+id/three"
android:title="Three"/>
</menu>
4. 엑티비티 수정
파일명 : MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button1 = findViewById<Button>(R.id.button1)
button1.setOnClickListener {
val popup = PopupMenu(this, button1)
popup.menuInflater.inflate(R.menu.poupup_menu, popup.menu)
popup.setOnMenuItemClickListener {
Toast.makeText( this, "clicked : "+it.title, Toast.LENGTH_SHORT).show()
return@setOnMenuItemClickListener true
}
popup.show()
}
}
}
5. 실행
댓글목록
등록된 댓글이 없습니다.