본문 바로가기

플밍 is 뭔들/안드로이드_인강

10-1. 액션바 & 옵션메뉴

※ 액션바
사진출처 : 서울위즈 앱 창업자를 위한 안드로이드 실무 동영상

※ 옵션메뉴
사진출처 : 서울위즈 앱 창업자를 위한 안드로이드 실무 동영상


※ 액션바와 옵션메뉴 구현
MainActivity
public class MainActivity extends Activity {
      TextView tv;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            tv = (TextView) findViewById(R.id.tv);
      }
      //액션바와 옵션메뉴를 초기화 해준다.
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
      }
      
      //액션바와 옵션메뉴의 리스너 부분, 이곳에서 기능을 구현한다.
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()){
            case R.id.action_menu_01 :
                  Toast.makeText(MainActivity.this, "action_01", Toast.LENGTH_SHORT).show();
                  break;
            case R.id.action_menu_02 :
                  Toast.makeText(MainActivity.this, "action_02", Toast.LENGTH_SHORT).show();
                  break;
            case R.id.action_menu_03 :
                  Toast.makeText(MainActivity.this, "action_03", Toast.LENGTH_SHORT).show();
                  break;
            case R.id.menu_01 :
                  Toast.makeText(MainActivity.this, "menu_01입니다.", Toast.LENGTH_SHORT).show();
                  break;
            case R.id.menu_02 :
                  Toast.makeText(MainActivity.this, "menu_02입니다.", Toast.LENGTH_SHORT).show();
                  break;
            case R.id.menu_03 :
                  Toast.makeText(MainActivity.this, "menu_03입니다.", Toast.LENGTH_SHORT).show();
                  break;
            }
            
            tv.setText(item.getTitle().toString());
            
            return false;
      }
}

activity_main 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidex2.MainActivity" >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</RelativeLayout>

main.xml (res/menu/main.xml 에 위치하여 있다)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.androidex2.MainActivity" >
     
          //액션바 아이템
      <item
          android:id="@+id/action_menu_01"
          android:icon="@drawable/ic_launcher"
          android:orderInCategory="1"
          android:title="action01"
          android:showAsAction="always"/>
      
      <item
          android:id="@+id/action_menu_02"
          android:icon="@drawable/ic_launcher"
          android:orderInCategory="2"
          android:title="action02"
          android:showAsAction="always"/>
      
      <item
          android:id="@+id/action_menu_03"
          android:icon="@drawable/ic_launcher"
          android:orderInCategory="3"
          android:title="action03"
          android:showAsAction="always"/>
          
          //옵션메뉴 아이템
      <item
          android:id="@+id/menu_01"
          android:title="menu01"/>
      <item
          android:id="@+id/menu_02"
          android:title="menu02"/>
      <item
          android:id="@+id/menu_03"
          android:title="menu03"/>
</menu>



'플밍 is 뭔들 > 안드로이드_인강' 카테고리의 다른 글

11-2. 팝업대화상자  (0) 2016.11.27
11-1. 팝업대화상자  (0) 2016.11.27
09-1. 프레그먼트  (0) 2016.11.27
08-1. 인텐트  (0) 2016.11.27
07-2. 액티비티 생명주기 테스트 및 로그 활용법  (0) 2016.11.27