자바 웹 개발자가 될거야/JAVA

[Android] 웹뷰, 메뉴 만들기

whitz 2021. 12. 23. 12:42

- res에는 이미 이름이 존재하는 폴더가 있음

- 메뉴를 만드려면 res 아래 menu 폴더를 만들어야 함

 

 

- 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 android:id="@+id/itemBlue"
        android:title="배경색(파랑)" />

    <item android:id="@+id/itemGreen"
        android:title="배경색(초록)" />

    <item android:title="버튼 변경 >>">
        <menu>
            <item android:title="버튼 45도 회전"
                android:id="@+id/subRotate"/>
            <item android:title="버튼 두배 확대"
                android:id="@+id/subSize"/>
        </menu>
    </item>

</menu>

 

 

- .java

 

package com.example.ex7_1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView tv1;
    LinearLayout baseLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = findViewById(R.id.tv1);
        baseLayout = findViewById(R.id.baselayout);
    }

    public boolean onCreateOptionsMenu(Menu menu){
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu1,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        super.onOptionsItemSelected(item);

        switch (item.getItemId()){
            case R.id.itemRed: baseLayout.setBackgroundColor(Color.RED);
                break;
            case R.id.itemBlue: baseLayout.setBackgroundColor(Color.BLUE);
                break;
            case R.id.itemGreen: baseLayout.setBackgroundColor(Color.GREEN);
                break;
            case R.id.subRotate: tv1.setRotation(45);
                break;
            case R.id.subSize: tv1.setScaleX(2);
                break;
        }

        return true;
    }
}

 

- 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"
    android:id="@+id/baselayout"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
         />

</LinearLayout>

 

 

45도 회전

 

- 클릭할때마다 45도 계속 돌리고 싶다면

 

case R.id.subRotate: tv1.setRotation(45+tv1.getRotation());
break;