Android Application
작성자 임베디드코리아
작성일23-11-16 11:49
조회1,363회
댓글0건
Kotlin 메뉴(Menu) : 그림 변경과 회전 하기
-------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/baseLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/jeju2" />
<EditText
android:id="@+id/edtAngle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1"
android:text="0" >
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/edtAngle"
android:layout_alignParentLeft="true"
android:text=" Է "
android:textSize="20dp" />
</RelativeLayout>
------------------------------------------------------------------------------------
MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
lateinit var edtAngle : EditText
lateinit var imageView1 : ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "제주도 풍경"
edtAngle = findViewById<EditText>(R.id.edtAngle)
imageView1 = findViewById<ImageView>(R.id.imageView1)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
super.onCreateOptionsMenu(menu)
val mInflater = menuInflater
mInflater.inflate(R.menu.menu1, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem) : Boolean {
when (item.itemId) {
R.id.itemRotate -> {
imageView1.rotation = java.lang.Float.parseFloat(edtAngle.text.toString())
return true
}
R.id.item1 -> {
imageView1.setImageResource(R.drawable.jeju2)
return true
}
R.id.item2 -> {
imageView1.setImageResource(R.drawable.jeju14)
return true
}
R.id.item3 -> {
imageView1.setImageResource(R.drawable.jeju6)
return true
}
}
return false
}
}
-------------------------------------------------------------------------------------
***** menu1.xml * ******
menu 폴더 만들기 --> New ---> Android Resource Directory
menu.xml 만들기 --> New ---> Menu Resource File
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/itemRotate"
android:title="그림 회전">
</item>
<group android:checkableBehavior="single" >
<item
android:id="@+id/item1"
android:checked="true"
android:title="한라산">
</item>
<item
android:id="@+id/item2"
android:title="추자도">
</item>
<item
android:id="@+id/item3"
android:title="범섬">
</item>
</group>
</menu>
-------------------------------------------------------------------------------------
***** themes.xml **********
-------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.MenuOptionmenu" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="colorPrimary">#8D8F9C</item>
<item name="colorPrimaryDark">#CDDC39</item>
</style>
<style name="Theme.MenuOptionmenu" parent="Base.Theme.MenuOptionmenu" />
</resources>