실습 7-2 배경색, 버튼 변경 앱
** menu 2개, 코딩 내용은 옵션 메뉴와 동일.
![]() |
![]() |
AVD 화면 |
<xml 코드> - 단순해서 접은글로 넣음.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/baseLayout">
<Button
android:id="@+id/btn1"
android:text="배경색 변경"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn2"
android:text="버튼 변경"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<java 코드>
package com.cookandroid.practice7_2;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
LinearLayout baseLayout;
Button btn1, btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("배경색 바꾸기(컨텍스트 메뉴)");
baseLayout = findViewById(R.id.baseLayout);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
registerForContextMenu(btn1);
registerForContextMenu(btn2);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater mInflater = getMenuInflater();
if(v == btn1) {
menu.setHeaderTitle("배경색 변경");
mInflater.inflate(R.menu.menu1, menu);
}
if(v == btn2) {
menu.setHeaderTitle("버튼 변경");
mInflater.inflate(R.menu.menu2, menu);
}
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
super.onContextItemSelected(item);
switch (item.getItemId()) {
case R.id.itemRed:
baseLayout.setBackgroundColor(Color.RED);
return false;
case R.id.itemGreen:
baseLayout.setBackgroundColor(Color.GREEN);
return false;
case R.id.itemBlue:
baseLayout.setBackgroundColor(Color.BLUE);
return false;
case R.id.subRotate:
btn2.setRotation(45);
return false;
case R.id.subSize:
btn1.setScaleX(2);
btn2.setScaleX(2);
return false;
} return true;
}
}
▶ 복습 개념
- (java) 순서 ** 옵션메뉴와 차이 있음!!
1. 메뉴 사용할 위젯 등록(onCreat() 안에 registerForContextMenu()로 등록)
2. 메뉴 파일 등록(onCreateContextMenu())
- 위젯별 컨텍스트 메뉴, if문 활용.
3. 메뉴 선택시 동작할 내용 코딩(onContextItemSelected())
** 오버라이드 메소드에서 꺼내 쓰기.
직접 풀어보기 7-2(실습 7-2 응용, java 코드로만 메뉴 만들기)
![]() |
![]() |
AVD 화면 |
<xml 코드, 위와 동일>
<java 코드>
package com.cookandroid.myself7_2;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
LinearLayout baseLayout;
Button btn1, btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("배경색 바꾸기(컨텍스트 메뉴)");
baseLayout = findViewById(R.id.baseLayout);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
registerForContextMenu(btn1);
registerForContextMenu(btn2);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v == btn1) {
menu.add(0, 1, 0, "배경색(빨강)");
menu.add(0, 2, 0, "배경색(초록)");
menu.add(0, 3, 0, "배경색(파랑)");
}
if (v == btn2) {
menu.add(0, 4, 0, "버튼 45도 회전");
menu.add(0, 5, 0, "버튼 2개 확대");
}
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
super.onContextItemSelected(item);
switch (item.getItemId()) {
case 1:
baseLayout.setBackgroundColor(Color.RED);
break;
case 2:
baseLayout.setBackgroundColor(Color.GREEN);
break;
case 3:
baseLayout.setBackgroundColor(Color.BLUE);
break;
case 4:
btn2.setRotation(45);
break;
case 5:
btn1.setScaleX(2);
btn2.setScaleX(2);
break;
} return true;
}
}
'모바일 > android' 카테고리의 다른 글
[안드로이드프로그래밍6판] 직접 풀어보기 9-1, 9-2, 실습 9-1 (0) | 2021.12.15 |
---|---|
[안드로이드프로그래밍6판] (대화 상자) 실습 7-3, 직접 풀어보기 7-3 (0) | 2021.12.11 |
[안드로이드프로그래밍6판] (옵션 메뉴)실습 7-1 고대로 베낌, 직접 풀어보기 7-1 (0) | 2021.10.22 |
[안드로이드프로그래밍6판] 6장 직접 풀어보기 6-2, 직접 풀어보기 6-3, 실습 6-2 / 뷰플리퍼, 탭호스트 (0) | 2021.10.18 |
[안드로이드프로그래밍6판] 6장 자동완성 텍스트뷰(고대로 베껴 봄) (0) | 2021.10.17 |