반응형

[TIP]

1. 이미지뷰의 이미지를 변경하려면 setImageResource() 메소드를 사용

2. 메뉴 XML에서 여러 항목을 묶으려면 <group>...</group>을 사용한다

 

[Gradle 설정]

1
id 'kotlin-android-extensions'
cs

 

<MainActivity>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.cookandroid.project7_1
 
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.SubMenu
import androidx.core.text.isDigitsOnly
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "애완동물 사진 고르기"
    }
 
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu1, menu)
        return super.onCreateOptionsMenu(menu)
    }
 
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        item.isChecked = !item.isChecked
        when(item.itemId){
            R.id.rotate ->{
                if(edit.text.isNotEmpty() && edit.text.isDigitsOnly()) img.rotation += edit.text.toString().toFloat()
            }
            R.id.itemDog ->{
                img.setImageResource(R.drawable.dog)
            }
 
            R.id.itemCat ->{
                img.setImageResource(R.drawable.cat)
            }
            R.id.itemRabbit ->{
                img.setImageResource(R.drawable.rabbit)
            }
        }
        return super.onOptionsItemSelected(item)
    }
 
 
}
cs

<activity_main.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@id/edit"
        android:text="각도 입력"
        android:textSize="20dp" />
 
    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/text"
        android:hint="숫자"
        android:textSize="20dp"/>
 
    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/dog"
        android:layout_centerInParent="true"
        android:scaleType="fitXY"/>
 
 
 
 
</RelativeLayout>
cs

 

<menu1.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
 
    <item
        android:id="@+id/rotate"
        android:title="그림 회전">
 
    </item>
 
    <group
        android:checkableBehavior="single">
        <item
            android:id="@+id/itemDog"
            android:checked="true"
            android:title="강아지">
        </item>
 
        <item
            android:id="@+id/itemCat"
            android:title="고양이">
        </item>
 
        <item
            android:id="@+id/itemRabbit"
            android:title="토끼">
        </item>
        </group>
 
</menu>
cs
반응형

+ Recent posts