2swan

Android 컨텍스트 메뉴 (배경색 변경) 본문

Programming/Android Example

Android 컨텍스트 메뉴 (배경색 변경)

2swan 2023. 8. 2. 23:29

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2_ContextMenu"
    android:orientation="vertical"
    android:id="@+id/baseLayout">
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="배경색 변경"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="버튼 변경"/>
</LinearLayout>

 

main.java

public class MainActivity2_ContextMenu extends AppCompatActivity {
    private LinearLayout baseLayout;
    private Button button1, button2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity2_context_menu);
        setTitle("배경색 바꾸기 (컨텍스트 메뉴)");
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        baseLayout = findViewById(R.id.baseLayout);
        registerForContextMenu(button1);
        registerForContextMenu(button2);

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater menuInflater = getMenuInflater();
        if(v == button1){
            menu.setHeaderTitle("배경색 변경");
            menuInflater.inflate(R.menu.menu1, menu);
        }
        if(v == button2){
            menu.setHeaderTitle("회전");
            menuInflater.inflate(R.menu.menu2, menu);
        }
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        if(item.getItemId() == R.id.itemRed){
            baseLayout.setBackgroundColor(Color.RED);
        }else if(item.getItemId() == R.id.itemGreen){
            baseLayout.setBackgroundColor(Color.GREEN);
        }else if(item.getItemId() == R.id.itemBlue){
            baseLayout.setBackgroundColor(Color.BLUE);
        }else if(item.getItemId() == R.id.subRotate){
            button2.setRotation(45);
        }else if(item.getItemId() == R.id.subSize){
            button2.setScaleX(2);
        }
        return false;
    }
}

 

res → menu 폴더 → menu1.xml

res → menu 폴더 → menu2.xml

//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>
            <item
                android:id="@+id/subSize"
                android:title="버튼 2배 확대">

            </item>
        </menu>
    </item>
</menu>
//menu2.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/subRotate"
        android:title="버튼 45도 회전"/>

    <item
        android:id="@+id/subSize"
        android:title="버튼 2배 확대 "/>

</menu>

결과 값

 

 

  • 버튼을 길게 눌러야 작동된다.
  • 버튼이 작동이 안될 시 → manifests →AndroidManifest.xml 파일에 접근해서 테마 속성을 변경 해본다.