Android 배경색 바꾸기(menu)

14160 단어 ExampleCodeExampleCode

1. activity_main.xml 코드

<androidx.appcompat.widget.LinearLayoutCompat
    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:orientation="vertical"
    android:id="@+id/baseLayout"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="오른쪽 위 메뉴 버튼을 누르세요"
        android:textSize="26dp"
        android:layout_gravity="center"/>
    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="이건 버튼"
        android:textSize="22dp"
        android:background="@android:color/holo_blue_bright"
        android:layout_gravity="center" />

</androidx.appcompat.widget.LinearLayoutCompat>

2. MainActivity.java 코드

public class MainActivity extends AppCompatActivity {
    private LinearLayoutCompat baseLayout;
    private Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("배경색 바꾸기");

        init();
    }
    public void init(){
        baseLayout = findViewById(R.id.baseLayout);
        btn1 = findViewById(R.id.btn1);
    }

    @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배 확대");
        sMenu.add(0, 6, 0, "버튼 원상복구");

        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 :
                btn1.setRotation(45);
                return true;
            case 5 :
                btn1.setScaleX(2);
                return true;
            case 6 :
                btn1.setRotation(0);
                btn1.setScaleX(1);
                return true;
        }
        return false;
    }
}

3. menu.xml 코드

<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/subRotateR"
                android:title="버튼 45도 회전" />
            <item
                android:id="@+id/subSizeUp"
                android:title="버튼 2배 확대" />
            <item
                android:id="@+id/subOriginal"
                android:title="버튼 원상복구" />
        </menu>
    </item>

</menu>

좋은 웹페이지 즐겨찾기