728x90

액션바 오른쪽위 메뉴인 옵션메뉴 뷰를 사용해보자.

onCreateOptionsMenu()메서드를 오버라이드 하여 옵션메뉴를 생성한다.

 

옵션메뉴 안에있는 메뉴를 클릭했을때 발생하는 이벤트처리는 

onOptionsItemSelected() 메서드를 오버라이드하여 처리한다. 

 

옵션메뉴를 생성하는 방법에는 두가지가있다. 

1. 메뉴xml을 이용해서 만드는방법과

2. kt파일에서 하드코딩하여 옵션을 만드는 방법이 있다.

 

1. menu1레이아웃을 먼저 만든다.

 

 

 

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    >
    
    <item
        android:id = "@+id/itemRed"
        android:title=" 배경색 (빨강)"
        tools:ignore="MissingConstraints">
    </item>



    <item
        android:id = "@+id/itemGreen"
        android:title=" 배경색 (초록)"
        tools:ignore="MissingConstraints">
    </item>


    <item
        android:id = "@+id/itemBlue"
        android:title=" 배경색 (파랑)"
        tools:ignore="MissingConstraints">
    </item>

    <item android:title="버튼변경 >> ">
        <menu>
            <item
                android:id="@+id/subRotate"
                android:title="버튼 45도 회전"/>
            <item
                android:id="@+id/subSize"
                android:title="버튼 2배 확대">
            </item>
        </menu>
    </item>
</menu>

 

파일명은 menu1.xml파일이다 

 

코드를 설명하면 menu1.xml파일은 3개의 아이템이 각각 존재하며

버튼변경>> 이라는 하나의 아이템속 메뉴안에 두개의 아이템이 존재한다.

item을 선언할때 꼭 id값과 title값을 까먹지말고 정의해야 한다.

 

 

이제 메인엑티비티.kt파일에서 오버라이드하여 메뉴를 활성시켜야된다.

 

 

 

 

 override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        super.onCreateOptionsMenu(menu)
        var mInflater = menuInflater
        mInflater.inflate(R.layout.menu1, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.itemRed ->{
                baselayout.setBackgroundColor(Color.RED)
                return true
            }
            R.id.itemBlue ->{
                baselayout.setBackgroundColor(Color.BLUE)
                return true
            }
            R.id.itemGreen ->{
                baselayout.setBackgroundColor(Color.GREEN)
                return true
            }
            R.id.subRotate ->{
                //돌리기
                button1.rotation = 45f
                return true

            }
            R.id.subSize -> {
                //확대


                button1.scaleX = 2f
                return true
            }
        }

        return super.onOptionsItemSelected(item)
    }

}

 

 

 

2. 하드코딩을 통한 옵션메뉴 만들기

 

옵션메뉴 만들기

override fun onCreateOptionsMenu(menu: Menu?): Boolean {

         menu!!.add(0, 1, 0, "배경색 (빨강)")
         menu!!.add(0, 2, 0, "배경색 (초록)")
         menu!!.add(0, 3, 0, "배경색 (파랑)")
         menu!!.add(0, 3, 0, "버튼 확대")
         menu!!.add(0, 3, 0, "버튼 돌리기")
         return super.onCreateOptionsMenu(menu)
     }

 

옵션메뉴 클릭이벤트 처리 

 

 

 

override fun onOptionsItemSelected(item: MenuItem): Boolean {
         when (item.itemId) {
             1 -> {
                 baselayout.setBackgroundColor(Color.RED)
                 return true
             }
             2 -> {
                 baselayout.setBackgroundColor(Color.BLUE)
                 return true
             }
             3 -> {
                 baselayout.setBackgroundColor(Color.GREEN)
                 return true
             }
             4->{
                //버튼확대
             }
             5->{
                 //버튼돌리기기
             }

         }
     return true
     }
728x90

+ Recent posts