본문 바로가기
App/Android Java

Android_java(12) - OptionMenuApp

by SeleniumBindingProtein 2022. 2. 9.
728x90
반응형

1. 메뉴의 종류
    1) Option menu : 액션바에 표시되는 메뉴 
    2) Context menu : 사용자가 화면을 일정 시간 이상 길게 누르면 나타남 
    3) Popup menu

2. Option menu
    1) 메뉴 생성
        - 메뉴 XML 파일 생성
            - <menu> 요소
                - Menu를 생성 
                - 메뉴 항목들을 담는 컨테이너가 됨 
                - 반드시 루트 노드이어야 함
                - <item>이나 <group>을 하나 이상 포함함 
            - <item> 요소
                - MenuItem을 생성함 
                - 하나의 메뉴 항목을 나타냄 
    2) 메뉴 파일 등록
        - onCreateOptionMenu()
        - 메뉴 리소스를 팽창(inflate)하면 실제 메뉴가 생성 
            - XML 파일을 읽어서 실제 메뉴로 만들어 주는 것을 팽창한다고 함 
    3) 메뉴 항목 이벤트 처리 (메뉴 선택 시 동작할 내용 코딩)
        - onOptionItemSelected()


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/baseLayout"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/jeju2"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edtAngle"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/textView"
        android:text="0">
    </EditText>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_alignBottom="@+id/edtAngle"
        android:layout_alignParentLeft="true"
        android:textSize="20dp"
        android:text="각도입력"
        />

</RelativeLayout>
package kr.co.optionmenuapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    EditText editText;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.drawable.ic_launcher);
        setTitle("제주도 풍경");

        editText = findViewById(R.id.edtAngle);
        imageView = findViewById(R.id.imageView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu, menu); // 메뉴 팽창
        return true;
    }

    //옵션 메뉴 클릭 이벤트 처리
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        //super.onOptionsItemSelected(item);
        switch (item.getItemId()){
            case R.id.itemRotate:
                imageView.setRotation(Float.parseFloat(editText.getText().toString()));
                return true;
            case R.id.item1:
                imageView.setImageResource(R.drawable.jeju2);
                return true;
            case R.id.item2:
                imageView.setImageResource(R.drawable.jeju4);
                return true;
            case R.id.item3:
                imageView.setImageResource(R.drawable.jeju6);
                return true;
        }
        return false;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/itemRotate"
        android:title="그림 회전">
    </item>
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/item1"
            android:checked="true"
            android:title="한라산">
        </item>
        <item
            android:id="@+id/item2"
            android:title="추자도">
        </item>
        <item
            android:id="@+id/item3"
            android:title="범섬">
        </item>
    </group>
</menu>

[결과 출력]

728x90
반응형

'App > Android Java' 카테고리의 다른 글

Android_Java(14) - FileApp  (0) 2022.02.09
Android_java(13) - AlertDialogApp  (0) 2022.02.09
Android_java(11) - TabHost  (0) 2022.02.08
Android_Java(10) -VeiwFlipperApp  (0) 2022.02.08
Android_Java(9) - DateTimeApp  (0) 2022.02.08

댓글