Android 프로그래밍 2

본문 바로가기
사이트 내 전체검색


Android 프로그래밍 2
Android 프로그래밍 2

7. Popup Menu

페이지 정보

작성자 관리자 댓글 0건 조회 1,246회 작성일 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. 실행



댓글목록

등록된 댓글이 없습니다.


개인정보취급방침 서비스이용약관 모바일 버전으로 보기 상단으로

TEL. 063-469-4551 FAX. 063-469-4560 전북 군산시 대학로 558
군산대학교 컴퓨터정보공학과

Copyright © www.leelab.co.kr. All rights reserved.