1. 메뉴 menu1.xml 로 메뉴추가
파일명 : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/baseLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="키패드의 MENU 버튼을 누르세요"
android:textSize="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "@+id/button1"
android:text="이건 버튼" />
</LinearLayout>
파일명 : menu1.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/itemRed"
android:title="배경색 (빨강)" >
</item>
<item
android:id="@+id/itemGreen"
android:title="배경색 (초록)" >
</item>
<item
android:id="@+id/itemBlue"
android:title="배경색 (파랑)" >
</item>
<item android:title="버튼 변경 >> ">
<menu>
<item
android:id="@+id/subRotate"
android:title="버튼 45도 회전" />
<item
android:id="@+id/subSize"
android:title="버튼 2배 확대" />
</menu>
</item>
</menu>
파일명 : MainActivity.java
package com.example.it.myapplication7_1;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
LinearLayout baseLayout;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("배경색 바꾸기");
baseLayout = (LinearLayout) findViewById(R.id.baseLayout);
button1 = (Button) findViewById(R.id.button1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mInflater = getMenuInflater();
mInflater.inflate(R.menu.menu1, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.itemRed:
baseLayout.setBackgroundColor(Color.RED);
return true;
case R.id.itemGreen:
baseLayout.setBackgroundColor(Color.GREEN);
return true;
case R.id.itemBlue:
baseLayout.setBackgroundColor(Color.BLUE);
return true;
case R.id.subRotate:
button1.setRotation(45);
return true;
case R.id.subSize:
button1.setScaleX(2);
return true;
}
return super.onOptionsItemSelected(item);
}
}
2. 자바코드로 메뉴 추가
파일명 : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/baseLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="키패드의 MENU 버튼을 누르세요"
android:textSize="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "@+id/button1"
android:text="이건 버튼" />
</LinearLayout>
파일명 : MainActivity.java
package com.example.it.myapplication7_2;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
LinearLayout baseLayout;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("배경색 바꾸기");
baseLayout = (LinearLayout) findViewById(R.id.baseLayout);
button1 = (Button) findViewById(R.id.button1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, 1, 0, "배경색(빨강)");
menu.add(0, 2, 0, "배경색(초록)");
menu.add(0, 3, 0, "배경색(파랑)");
SubMenu sMenu = menu.addSubMenu("버튼 변경 >>");
sMenu.add(0, 4, 0, "버튼 45도 회전");
sMenu.add(0, 5, 0, "버튼 2배 확대");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
baseLayout.setBackgroundColor(Color.RED);
return true;
case 2:
baseLayout.setBackgroundColor(Color.GREEN);
return true;
case 3:
baseLayout.setBackgroundColor(Color.BLUE);
return true;
case 4:
button1.setRotation(45);
return true;
case 5:
button1.setScaleX(2);
return true;
}
return false;
}
}
'IT > Android' 카테고리의 다른 글
android - menu, imageView 응용 (0) | 2018.09.20 |
---|---|
android - context 메뉴 추가 (0) | 2018.09.20 |
android - 안드로이드 웹뷰(WebView) (0) | 2018.09.19 |
android - 액션바(ActionBar)와 프래그먼트(Fragment)를 활용하여 화면 구현 (0) | 2018.09.19 |
android - TabHost와 LinearLayout, TabHost와 ImageView (0) | 2018.09.17 |