1. ์์ ฏ
: View์ ์๋ธ ํด๋์ค ์ค ํ๋ฉด์ ๋ณด์ด๋ ๊ฒ๋ค
- ex. TextView, EditText, Button
2. view
: ๋ชจ๋ UI ์ปดํฌ๋ํธ๋ค์ ๋ถ๋ชจ ํด๋์ค (View ํด๋์ค์ ์์ฑ์ ๋ชจ๋ UI ์ปดํฌ๋ํธ๋ค์์ ๊ณตํต์ ์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅ)
1) id : UI ์ปดํฌ๋ํธ๋ฅผ ๊ณ ์ ํ๊ฒ ์๋ณํ๋ ์๋ณ์ (์ค๋ณต๋ ์ ์์)
(1) ์๋ณ์ ์ง์ ํ์
android:id="@+id/my_button"
(2) ์๋ณ์ ์ฐธ์กฐ ํ์
android:id="@id/my_button"
2) layout_width, layout_height
(1) match_parent ๋๋ fill_parent : ๋ถ๋ชจ UI ์ปดํฌ๋ํธ์ ํฌ๊ธฐ์ ๋ง์ถค
(2) wrap_content : UI ์ปดํฌ๋ํธ์ ๋ด์ฉ๋ฌผ ํฌ๊ธฐ์ ๋ง์ถค
// px (pixel) ํ์์ผ๋ก view์ ํฌ๊ธฐ ์ค์ ์ ๋์คํ๋ ์ด ํด์๋์ ๋ฐ๋ผ view์ ํฌ๊ธฐ๊ฐ ๋ฌ๋ผ๋ณด์
// dp ํ์์ ๋ฐ๋์ ๋ ๋ฆฝ์ ์ธ ๋จ์. 1dp๋ ๋ฐ๋๊ฐ 160dpi์ ํ๋ฉด์ผ ๋ 1ํฝ์ ์ ๋ํ๋ (์๋์ผ๋ก ๋ง์ถฐ์ง)
// ๋ค์ํ ํด์๋ ๋์ ๋ฐฉ๋ฒ -> ๋ค์ํ ์ฌ์ด์ฆ์ ์ด๋ฏธ์ง๋ฅผ ์ค๋นํด์ (5์ฅ) ๋ชจ๋ drawable์ ๋ฃ์ด์ผ ํจ
// 9-PATCH ์ด๋ฏธ์ง : ์ด๋ฏธ์ง๊ฐ ๊ทธ๋ ค์ง ์์ญ์ ํฌ๊ธฐ๊ฐ ๋์ด๋๊ฑฐ๋ ์ค์ด๋ค์ด๋ ์๋ณธ์ด๋ฏธ์ง ํํ๋ฅผ ์ ์งํ๋๋ก ๋ ๊ฒ
3. TextView
: ํ๋ฉด์ text๋ฅผ ํ์ํ๋ ์ฉ๋
- ์ฃผ์ ์์ฑ
1) View ์์ฑ ์์ : id, layout_width, layout_height, background, etc
2) text : ์ถ๋ ฅํ ๋ฌธ์์ด ์ง์
3) textSize : ํฐํธ ํฌ๊ธฐ
4) textStyle : ํ ์คํธ ์คํ์ผ (bold, italic, normal)
5) typeface : ํ ์คํธ ํฐํธ (normal, sans, serif, monspace)
6) textColor : ๋ฌธ์์ด ์์
7) singleLine : ์์ฑ๊ฐ์ด "true" ์ด๋ฉด, ํ ์คํธ๊ฐ ์์ ฏ์ ํญ๋ณด๋ค ๊ธธ ๋ ๊ฐ์ ๋ก ํ ์ค์ ์ถ๋ ฅ
4. EditText
: ์ ๋ ฅ์ด ๊ฐ๋ฅํ Text์ฐฝ
- TextView์ ๋ชจ๋ ์์ฑ ์์ (EditText๋ TextView์ ์๋ธํด๋์ค)
- inputType : ์ ๋ ฅ ์ ํ์ฉ๋๋ ํค๋ณด๋ ํ์ ์ค์ ๋ฐ ํค๋ณด๋ ํ์๋ฅผ ์ค์
1) ํค๋ณด๋ ํ์ ์ค์ ๊ฐ
(1) "text" : ์ผ๋ฐ์ ์ธ ํ ์คํธ ํค๋ณด๋
(2) "phone" : ์ ํ๋ฒํธ ์ ๋ ฅ ํค๋ณด๋
(3) "textEmailAddress" : @๋ฌธ์๋ฅผ ๊ฐ์ง ํ ์คํธ ํค๋ณด๋
2) ํค๋ณด๋ ํ์ ์ค์ ๊ฐ
(1) "textCapWords" : ๋ฌธ์ฅ์ ์์์ ๋๋ฌธ์๋ก ๋ณํ
(2) "textAutoCorrect" : ์ ๋ ฅ๋ ๋จ์ด์ ์ ์ฌํ ๋จ์ด๋ฅผ ์ ์ํ๊ณ ์ ์๋ ๋จ์ด ์ ํ ์, ์ ๋ ฅ๋ ๋จ์ด๋ฅผ ๋์น
(3) "textMultiLine" : ์ฌ๋ฌ ์ค์ ์ ๋ ฅ๋ฐ์ ์ ์์
// background : ๋ทฐ์ ๋ฐฐ๊ฒฝ์ ์ง์ , ์์ ๋ฐ ์ด๋ฏธ์ง ๋ฑ์ ์ฌ๋ฌ ๊ฐ์ฒด๋ก ์ง์ ๊ฐ๋ฅ
5. Button
: ์ฌ์ฉ์๊ฐ ๋ฒํผ์ ๋๋ ์ ๋ ์ด๋ค ํ๋์ ์ํํ๊ฒ ํจ
- Button ํด๋์ค๋ TextView์ ์๋ธํด๋์ค. TextView์ ๋ชจ๋ ์์ฑ ์ฌ์ฉ ๊ฐ๋ฅ (ex. singleLine)
- ๋ฒํผ ๋ด์ ํ ์คํธ, ์์ด์ฝ ํ์ ๊ฐ๋ฅ (๋ฒํผ ์ ์ฒด๋ฅผ ์ด๋ฏธ์ง๋ก ๊ทธ๋ฆฌ๊ธฐ ์ํด์๋ ImageButton ์ฌ์ฉ)
(1) ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ 1 (๋ฒํผ ์์ ฏ์ onClick ์์ฑ ํ์ฉ - doAction())
: ๋ฒํผ ์์ ฏ์ ์ ์ํ ํ๋ฉด์ contentView๋ก ์ค์ ํ ์กํฐ๋นํฐ ํด๋์ค์ ์๋ก์ด ๋ฉ์๋ (ex. doAction() ์ถ๊ฐ)
-> ๋ฒํผ ์์ ฏ์ ์ ์ํ xml ๋ ์ด์์ ํ์ผ(ex. text_views.xml)์์, ๋ฒํผ ์์ ฏ์ onClick ์์ฑ์ ์ ๋จ๊ณ์์ ์ถ๊ฐํ ๋ฉ์๋๋ฅผ ์ค์
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText Test"
android:textColor="#ff0000"
android:textSize="10pt"
android:typeface="serif"
android:textStyle="bold"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ID"
android:inputType="text"
android:textSize="10pt"
android:textStyle="italic"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="PassWord"
android:inputType="textPassword"
android:textSize="10pt"
android:textStyle="italic"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Telephone"
android:inputType="phone"
android:textSize="10pt"
android:textStyle="italic"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:textSize="10pt"
android:textStyle="italic"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Description"
android:inputType="text|textMultiLine|textAutoCorrect"
android:textSize="10pt"
android:textStyle="italic"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:onClick="doAction"/> //doAction ์ฌ์ฉ
</LinearLayout>
package com.example.uibasic
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.text_views)
// ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
// val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
// v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
// insets
// }
}
fun doAction(v: View){
//text_views์์ ๋ง๋ doAction
Toast.makeText(getApplicationContext(), "Submitted Successfully", Toast.LENGTH_SHORT).show()
}
}
(2) ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ 2 (์ด๋ฒคํธ ์ฒ๋ฆฌ ๊ฐ์ฒด ์ฌ์ฉ)
: ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ๋ ๊ฐ์ฒด๋ฅผ ์์ฑ, ํด๋น ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํค๋ ์์ ฏ์ ๋ฑ๋ก
- ์์ ฏ์์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด ๋ฑ๋ก๋ ์ด๋ฒคํธ ์ฒ๋ฆฌ ๊ฐ์ฒด๊ฐ ์ ์๋ ์ผ์ ์ํํจ
- ๋ฒํผ์ด ํด๋ฆญ๋์์ ๋ ๋ฐ์ํ๋ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด View.OnClickListener ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ ํด๋์ค๋ฅผ ์ ์ -> ๊ตฌํํ ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์์ฑํด ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํค๋ ๋ฒํผ ์์ ฏ์ ๋ฑ๋ก
- ๋ฒํผ ์์ ฏ์ ์ ์ํ ๋ ์ด์์ ํ์ผ(ex. text_views.xml)์์ Button ๊ฐ์ฒด๋ฅผ ์ฝํ๋ฆฐ ์ฝ๋์์ ์ฐธ์กฐํ๊ธฐ ์ํด ๋ฒํผ ์์ ฏ์ id์์ฑ ์ถ๊ฐ
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
package com.example.uibasic
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.text_views)
//text_views.xml์ ์ ์๋ view ๊ฐ์ฒด ์ค์์ id๊ฐ submit_button์ธ ๊ฒ์ ์ฐพ์ ๋ฐํํจ
val btn = findViewById<Button>(R.id.submit_button)
//๋ฒํผ์ด ํด๋ฆญ๋์์ ๋ ๋ฐ์๋๋ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด์๋ View.OnClickListener ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํ
btn.setOnClickListener{
Toast.makeText(this,"submitted successfully", Toast.LENGTH_SHORT).show()
}
}
}
// findViewById( )
: Activity ํด๋์ค์ ์ ์๋ ๋ฉ์๋๋ก, Activity ํ์ ํด๋์ค(ex. AppCompatActivity)์์ ์ฌ์ฉ ๊ฐ๋ฅ
- ํด๋น ์กํฐ๋นํฐ์ ์ฐ๊ฒฐ๋ XML layout ๋ฆฌ์์ค ์์ (์์ ฏ) ์ค์์ id์์ฑ์ ๋ฐํ์ผ๋ก ํด๋น java ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ด
- onCreate() ๋ฉ์๋ ๋ด์ setContentView() ๋ฅผ ํตํด์ ์ฐ๊ฒฐ๋ XML ๋ฆฌ์์ค ์์ ์ค์์๋ง ๊ฒ์์ด ๊ฐ๋ฅํจ
-> ๋ค๋ฅธ ์กํฐ๋นํฐ์ ์ฐ๊ฒฐ๋ XML layout ๋ฆฌ์์ค์ ์ ์๋ ์์ ฏ์ findViewById( ) ๋ฉ์๋๋ก ๊ฐ์ ธ์ฌ ์๋ ์์
6. ImageView
: ์ฑ ํ๋ฉด์ ์ด๋ฏธ์ง๋ฅผ ํ์ํ๋ ์ฉ๋
[1] layout ๋ฆฌ์์ค XML ํ์ผ์ ImageView๋ฅผ ์ถ๊ฐ
- "src" ์์ฑ์ ์ด๋ฏธ์ง(drawable)์ ๋ฆฌ์์ค ID๋ฅผ ์ง์
- android : src-Image View์ ํ์ ๋ Drawable ์ง์
(@drawable/xxx์ ํ์์ผ๋ก Drawable ๋ฆฌ์์ค ์ง์ ๊ฐ๋ฅ, ์ปฌ๋ฌ๊ฐ ์ฌ์ฉ ๊ฐ๋ฅ)
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView1"
android:scr="@drawable/android_icon"/>
[2] ํ๋ฉด์ ํ์ํ ์ด๋ฏธ์ง๋ฅผ Drawable ๋ฆฌ์์ค์ ์ถ๊ฐ
- ์ด๋ฏธ์ง ํ์ผ์ ํ์์ .png๋ฅผ ๋ง์ด ์ฌ์ฉ (ํฌ๋ช
๋)
- ์ด๋ฏธ์ง ํ์ผ์ /res/drawable์ ์ถ๊ฐ
- ํด์๋์ ๋ฐ๋ฅธ ๋ค๋ฅธ ํฌ๊ธฐ์ ์ด๋ฏธ์ง๋ ๋ณ๋์ ํด๋๋ฅผ ์์ฑํ๊ณ ๋ณต์ฌ (ex. drawable-xhdpi ๋ฑ...)
- ํ๊ธ ํฌํจ ๋ถ๊ฐ, ๋๋ฌธ์ ํฌํจ ๋ถ๊ฐ, ์ซ์๋ก ์์ ๋ถ๊ฐ // _๋ก ์์ ๊ฐ๋ฅ
[3-1] ํ๋ฉด์ ํ์ํ ์ด๋ฏธ์ง(Drawable)๋ฆฌ์์ค id๋ฅผ ImageView์ "src" ์์ฑ์ ์ง์
[3-2] Kotlin ์์ค์์ ImageView์ ์ด๋ฏธ์ง ๋ณ๊ฒฝ
: ImageView์ id๋ฅผ ์ด์ฉํด ์ฐธ์กฐ๋ฅผ ํ๋ํ ํ, setImageResource( ) ํจ์ ํธ์ถ
class MainActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var imageView: ImageView = findViewById(R.id.imageView1)
imageView.setImageResource(R.drawable.google_logo)
}
}
[4] ImageView์ ์์ญ์ ๋ง๊ฒ ์ด๋ฏธ์ง ํ๋ ๋๋ ์ถ์
: scaleType ์์ฑ > android:scaleType
(1) android:scaleType = "Center"
: ์ด๋ฏธ์ง์ ํฌ๊ธฐ, ๋น์จ์ ์ ์งํ๋ฉฐ ์ด๋ฏธ์ง์ ์ค์์ ImageView์ ์ค์ฌ์ ๋ง์ถค
(ImageView๋ณด๋ค ์ด๋ฏธ์ง๊ฐ ํด ๊ฒฝ์ฐ ์ด๋ฏธ์ง๊ฐ ์๋ฆผ)
(2) android:scaleType = "centerCrop"
: ์ด๋ฏธ์ง์ ๋น์จ์ ์ ์งํ๋ฉฐ ๊ฐ๋ก, ์ธ๋ก ์ค ์งง์ ์ชฝ์ ImageView์ ๊ฝ ์ฐจ๊ฒ ์ถ๋ ฅ
(ImageView๋ฅผ ๋ฒ์ด๋๋ ๋ถ๋ถ์ ์ถ๋ ฅ๋์ง ์์)
(3) android:scaleType = "centerInside"
: ์ด๋ฏธ์ง์ ๊ฐ๋ก, ์ธ๋ก ์ค ๊ธด ์ชฝ์ ImageView์ ๋ ์ด์์์ ๋ง์ถฐ ์ถ๋ ฅ
(์ด๋ฏธ์ง์ ๋น์จ ์ ์ง๋จ, ๋จ๋ ๊ณต๊ฐ์ background์ ์์ผ๋ก ์ฑ์์ง)
- fitCenter๊ณผ ๋ฌ๋ฆฌ, ์ด๋ฏธ์ง๊ฐ ImageView๋ณด๋ค ์์ ๊ฒฝ์ฐ ํฌ๊ธฐ ์ ์ง๋จ
(4) android:scaleType = "fitCenter"
: centerInside์ ์ ์ฌ
(๋จ, ์ด๋ฏธ์ง์ ํฌ๊ธฐ๊ฐ ImageView๋ณด๋ค ์๋ค๋ฉด ImageView์ ํฌ๊ธฐ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง)
(5) android:scaleType = "fitStart"
: ImageView ์์์ ๊ฐ๋ก, ์ธ๋ก ๋น์จ์ ์ ์งํ๋ฉฐ fitํ๊ฒ ์ถ๋ ฅ๋จ
(๋จ, fitCenter๊ณผ ๋ค๋ฅด๊ฒ ์ผ์ชฝ ์๋จ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ๋จ)
(6) android:scaleType = "fitEnd"
: fitStart์ ๋ง์ฐฌ๊ฐ์ง๋ก ๊ฐ๋ก, ์ธ๋ก ๋น์จ์ ์ ์งํ๋ฉฐ fitํ๊ฒ ์ถ๋ ฅ๋จ
(์ฐ์ธก ํ๋จ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ)
(7) android:scaleType = "fitXY"
: ๊ฐ๋ก, ์ธ๋ก ๋น์จ์ ์๊ด์์ด ImageView์ ๊ฝ ์ฐจ๊ฒ ๊ทธ๋ ค์ง
(์ด๋ฏธ์ง๊ฐ ์ฐ๊ทธ๋ฌ์ง ์ํ๋ก ๋ณด์)
(8) android:scaleType = "matrix"
: ์ด๋ฏธ์ง์ ํฌ๊ธฐ, ๋น์จ์ ์ ์งํ๋ฉฐ ImageView์ ์ข์ธก ์๋จ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
(์ด๋ฏธ์ง๊ฐ ํฌ๋ค๋ฉด ImageView ์ธ์ ๋ถ๋ถ์ ์ถ๋ ฅ๋์ง ์์)