์๋๋ก์ด๋๊ฐ ํ๋ก ํธ์๋ ์ดํ๋ฆฌ์ผ์ด์
์ด๋ค ๋ณด๋ UI๋ ๋ฐ๋์ ์ ๊ณตํด์ผ ํจ
UI๋ฅผ ์ ๊ณตํ๊ธฐ ์ํ ๋ง์ API์ ๊ธฐ๋ฒ๋ค์ด ์์
10๊ฐ Activity - View ๊ตฌ์กฐ
ํ๋ฉด ๊ตฌ์ฑ ๋ฐฉ๋ฒ
• ํ๋ฉด์ ์ถ๋ ฅํ๊ธฐ ์ํด์๋ Activity ๋ผ๊ณ ํ๋ ํด๋์ค๋ฅผ ๋ง๋ค์ด์ค์ผ ํจ.
๊ทธ๋ฆฌ๊ณ ๋ฐํ์ ์์ ์คํ๋๊ฒ๋ ํด์ฃผ์ด์ผ ํจ
์ด ํด๋์ค๋ ๊ฐ๋ฐ์๊ฐ ๋ง๋๋ ํด๋์ค(์กํฐ๋นํฐ๋ฅผ ์์๋ฐ์์ ๋ง๋๋ ํด๋์ค)
• Activity ๋ ํ๋ฉด ์ถ๋ ฅ ๋ฅ๋ ฅ์ ๊ฐ์ง๋ ์ปดํฌ๋ํธ
์ปดํฌ๋ํธ๋? (์ด 4๊ฐ์ง)
๊ฐ๋ฐ์๊ฐ ๋ง๋ ํด๋์ค์ด์ง๋ง, ์๋๋ก์ด๋ ์์คํ
์ ์ํด์ ๋ฐํ์์์ ๊ฐ์ฒด ์์ฑ ๋์ด
๋ผ์ดํ ์ฌ์ดํด ๊ด๋ฆฌ๊ฐ ๋๋ ํด๋์ค
ex) Activity, Service, ContentProvider, BroadcastReceiver
(๊ฐ๊ฐ ์กํฐ๋นํฐ๋ฅผ ์์๋ฐ์์ ๋ง๋๋ ํด๋์ค, ์๋น์ค๋ฅผ ์์๋ฐ์์ ๋ง๋๋ ํด๋์ค, ์ฝํ
ํธํ๋ก๋ฐ์ด๋๋ฅผ ์์๋ฐ์์ ๋ง๋๋ ํด๋์ค, ๋ธ๋ก๋์บ์คํธ๋ฆฌ์๋ฒ๋ฅผ ์์๋ฐ์์ ๋ง๋๋ ํด๋์ค)
• ํ๋ฉด ๊ตฌ์ฑ์ View ํด๋์ค๋ฅผ ์ด์ฉํ๋ค. (Activity ์์ฒด๋ ๋ฐฑ์ ๋ํ์ง)
• TextView, ImageView, Button, EditText ๋ฑ ๋ค์ํ View ํด๋์ค๋ค์ด ์ ๊ณต๋๋ค.
• Activity ์์ setContentView( ) ํจ์๋ฅผ ์ด์ฉํด View ๊ฐ์ฒด๋ฅผ Activity ํ๋ฉด์ ์ถ๋ ฅ
View ํด๋์ค
• TextView : ๋ฌธ์์ด ์ถ๋ ฅ View ํด๋์ค
• ImageView : ์ด๋ฏธ์ง ์ถ๋ ฅ View ํด๋์ค
• EditText : ์ ์ ๊ธ ์
๋ ฅ View ํด๋์ค
• Button : ๋ฒํผ View ํด๋์ค
์ด์ธ์๋ ์๋นํ ๋ง๊ณ , ์๋๋ก์ด๋์คํ๋์ค ๋ฒ์ ์ ๊ทธ๋ ์ด๋ ๋๋ฉด์๋ ๊ณ์ ์ถ๊ฐ๋จ
• View : ๋ชจ๋ View ํด๋์ค๋ค์ ์ต์์ ํด๋์ค.
• ViewGroup : ๋ค๋ฅธ View ๊ฐ์ฒด๋ฅผ ํ๊บผ๋ฒ์ ์ ์ดํ๊ธฐ ์ํ Container ๊ธฐ๋ฅ. (๋ทฐ์ ์๋ธ๋๊น ๋น์ฐํ๊ฒ ์ง๋ง, ์กํฐ๋นํฐ ํ๋ฉด์ ์ถ๋ ฅ ๊ฐ๋ฅ. ์ด ๋ทฐ๊ทธ๋ฃน์ด ์ถ๋ ฅ๋๋ฉด ์์ฒด UI๋ ๊ฐ์ง์ง ์์ -> ํ๋ฉด์ ์๋ฌด๊ฒ๋ ์ ๋์ด / ์ฐ๋ ์ด์ : ๋ค๋ฅธ ๋ทฐ ๊ฐ์ฒด ์ฌ๋ฌ ๊ฐ๋ฅผ ์ด ๋ทฐ๊ทธ๋ฃน์ Add ์์ผ์ ํ๊บผ๋ฒ์ ์ ์ดํ๊ธฐ ์ํจ)
• TextView, ImageView, EditText, Button ๋ฑ…
11๊ฐ Layout XML ์ ์ด์ฉํ ํ๋ฉด ๊ตฌ์ฑ
(1) Activity ์ฝ๋์์ ์ง์ ํ๋ฉด ๊ตฌ์ฑ
• View ํด๋์ค๋ฅผ Activity ์์ ์ง์ ์์ฑํด ํ๋ฉด ๊ตฌ์ฑ.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
//...............................
val image = ImageView(this).also {
it.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.logo_1))
}
val layout = LinearLayout(this).apply {
//.................
}
setContentView(layout)
}
}
Activity๋ฅผ ๋ง๋ค์ด์ผ ํ๋ฉด์ด ์คํ๋๊ณ , ์ด Activityํ๋ฉด์ View ํด๋์ค๋ก ๊ตฌํํด ์ฃผ์ด์ผ ํจ
AppCompatActivity()๋ฅผ ์์๋ฐ์ MainActivity๋ผ๋ Activity๊ฐ ๋ง๋ค์ด์ ธ ์์
์ด AppCompatActivity๊ฐ MainActivity์ ์๋ธ ํด๋์ค์ด์ ์กํฐ๋นํฐ ํด๋์ค๊ฐ ๋จ
๋ฐํ์ ์์ ์ด ์กํฐ๋นํฐ ํด๋์ค๊ฐ ์คํ์ด ๋๋ฉด, ์ด ์์์ ํ๋ฉด์ด ๋์ค๋๋ฐ,
ํ๋ฉด์ ์ด๋ป๊ฒ ๊ตฌ์ฑํ ๊ฒ์ด๋ ๋ช
๋ น์ ๋ด๋ฆฌ๊ธฐ ์ํด์
SetContentView()๋ฅผ ์๋์ ์ฌ์ฉํ๊ณ ,
์ด SetContentView ํจ์์ ๋งค๊ฐ๋ณ์์ ๋์ง ๋ถ๋ถ์ด ๊ฒฐ๊ตญ ์กํฐ๋นํฐ ํ๋ฉด์ ๋์ค๊ฒ ๋จ(layout)
(1) Code - ์กํฐ๋นํฐ ์ฝ๋๋ด์์ ๋ทฐ๊ฐ์ฒด๋ฅผ ์ง์ ์์ฑํด์ ์ค๋นํ๋ ๋ฐฉ๋ฒ ์ด ์๊ณ
(2) Layoutxml - Layout Xml ํ์ผ์ ์ด์ฉํด์ ํ๋ฉด์ ์ค๋นํ๋ ๋ฐฉ๋ฒ์ด ์์
์ฝ๋์์ ์ง์ ์ค๋นํ๋ ๋ฐฉ๋ฒ์ด๋ผ๋ ๊ฒ์ ์ฐ๋ฆฌ๊ฐ ์์ฑํ๋ ์กํฐ๋นํฐ ์ฝ๋๋ด์์
ํ๋ฉด ์ถ๋ ฅํ๊ธฐ ํ ๋ทฐ๋ค์ ์ง์ ์์ฑํด์ (ImageView, LinearLayout) ์ด๊ฑธ layout์ผ๋ก ๋ณด๋ธ ๊ฒ
MainActivity ๋ด์ ํ ์คํธ๋ทฐ/์ด๋ฏธ์ง๋ทฐ/ํ ์คํธ๋ทฐ ๊ฐ์ฒด ์์ฑ ๋ฐ ๊ตฌ์ฑ
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val name = TextView (this).apply {
typeface = Typeface.DEFAULT_BOLD
text = "Save the Earth"
}
val image = ImageView(this).also {
it.setImageDrawable(ContextCompat.getDrawable(
this, R.drawable.logo_1
))
}
val title = TextView (this).apply {
typeface = Typeface.DEFAULT_BOLD
text = "์ง๊ตฌ๋ฅผ ์ง์ผ์ฃผ์ธ์!"
}
val layout = LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
gravity = Gravity.CENTER
// LinearLayout์ ๊ฐ๋ก๋ฐฉํฅ ์ธ๋ก๋ฐฉํฅ ๋์ดํด์ฃผ๋ ๋ ์ด์์
addView(name, WRAP_CONTENT, WRAP_CONTENT)
addView(image, WRAP_CONTENT, WRAP_CONTENT)
addView(title, WRAP_CONTENT, WRAP_CONTENT)
// name, image, title ๋ทฐ๋ฅผ ์ ๋๋ทฐ
}
//์ต์ข
์ ์ผ๋ก activity๋ฅผ ์ถ๋ ฅํ๊ธฐ ์ํด setContentView๋ฅผ ์ฌ์ฉํด์ผ ํ๊ณ ,
// ํจ์์ ๋งค๊ฐ๋ณ์์ layout์ ๋์ง๋ฉด ๊ฒฐ๊ตญ ์ด๊ฑธ ์ถ๋ ฅํ๊ฒ ๋จ.
setContentView(layout)
}
}
(2) Layout XML ์์ ํ๋ฉด ๊ตฌ์ฑ
• Layout XML ์ฝ๋์ ํ๋ฉด ๊ตฌ์ฑ์ ์ํ View ๋ฅผ ํ๊ทธ๋ก ๋ฑ๋ก
์ถ๋ ฅํ๊ณ ์ ํ๋ ๋ทฐ ํด๋์ค๋ช
๊ณผ ๋์๋ฌธ์๊น์ง ๋์ผํ ํ๊ทธ๋ช
์ ์ฐ๋ฉด ๋จ
ํ๋ฉด์ ๋ฌธ์์ด์ ์ถ๋ ฅํ๊ฒ ๋ค๋ฉด ํ๊ทธ๋ช
์ด TextView์ธ xmlํ์ผ์ ์ ์ํด์,
์ด xmlํ์ผ์ ์กํฐ๋นํฐ ์ฝ๋์๋ค๊ฐ ํ๋ฉด์ ๋์๋ผ ๋ช
๋ น์ ๋ด๋ ค์ ์ถ๋ ฅ ์ํฌ ์๋ ์์
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android ……………………… >
<TextView ……………………….. />
</LinearLayout>
res - layout - activty_main.xml์ ์์ฑ
์๊น MainActivity ์ฝ๋์์ ์์ฑํ๋ ๊ฒ์ ๊ทธ๋๋ก xmlํ์ผ๋ก ๋๋ ค์ฃผ๋ฉด ๋จ
์์๋ฅผ LinearLayout์ผ๋ก ์์ , ์์ฑ ์ง์ android:
12๊ฐ ๊ธฐ์ด ์์ฑ
id ์์ฑ
•Layout XML ์ ๋ฑ๋ก๋๋ View ๊ฐ์ฒด ์๋ณ์
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"/>
•findViewById( ) ํจ์๋ก View ๊ฐ์ฒด ํ๋
val textView1: TextView = findViewById(R.id.text1)
layout_width, layout_height ์์ฑ
•๋ทฐ์ ์ฌ์ด์ฆ๋ฅผ ์ง์ , ํ์ ์์ฑ
•match_parent, wrap_content, 100dp
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:backgroundTint="#ff0000"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button2"
android:backgroundTint="#00ff00"/>
<Button
android:layout_width="150dp"
android:layout_height="150dp"
android:text="Button3"
android:backgroundTint="#0000ff"/>
</LinearLayout>
margin, padding ์์ฑ
•margin ์ ๋ทฐ์ ๋ทฐ ์ฌ์ด์ ๊ฐ๊ฒฉ, padding์ ๋ทฐ์ ์ปจํ
์ธ ์ ๋ทฐ ํ
๋๋ฆฌ ์ฌ์ด์ ๊ฐ๊ฒฉ
•padding, margin ์์ฑ์ ์ด์ฉํ๋ฉด 4๋ฐฉํฅ ๋ชจ๋ ๋์ผํ ์ฌ์ด์ฆ๋ก ๊ฐ๊ฒฉ์ด ์ค์
•ํ ๋ฐฉํฅ์ ๊ฐ๊ฒฉ๋ง ์ค์ ํ๊ณ ์ถ๋ค๋ฉด paddingLeft, paddingRight, paddingTop, paddingBottom์ layout_marginLeft, layout_marginRight, layout_marginTop, layout_marginBottom ์์ฑ์ ์ด์ฉ
visibility ์์ฑ
•visibility ์์ฑ์ ๋ทฐ๊ฐ ํ๋ฉด์ ์ถ๋ ฅ๋์ด์ผ ํ๋์ง์ ๋ํ ์ค์
•visible(default), invisible, gone ๊ฐ์ผ๋ก ์ค์
•invisible ๊ณผ gone ์ ๋ชจ๋ ํ๋ฉด์ ๋ทฐ๊ฐ ๋ณด์ด์ง ์๊ฒ ํ์ง๋ง ์ฌ์ด์ฆ๋ฅผ ํ๋ณดํ๋์ง์ ๋ํ ์ฐจ์ด๊ฐ ์๋ค.
13๊ฐ TextView
TextView ์์ฑ
•android:text – TextView ์ ์ถ๋ ฅ๋๋ ๋ฌธ์์ด ์ง์
•android:textColor – ๋ฌธ์์ด ์ปฌ๋ฌ
•android:textSize – ๋ฌธ์์ด ์ฌ์ด์ฆ
•android:textStyle – ๋ฌธ์์ด ์คํ์ผ
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="helloworld"
android:textColor="#FF0000"
android:textSize="20sp"
android:textStyle="bold"/>
•android:autoLink
•TextView ์ ์ถ๋ ฅ๋๋ ๋ฌธ์์ด์ ๋ถ์ํด ์๋ ๋งํฌ ์ถ๊ฐ
•web, email, phone ๋ฑ์ ๊ฐ์ผ๋ก ์ง์
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SeSAC - ์นํ์ด์ง : https://ssac.seoul.kr/, ์ ํ๋ฒํธ : 02-3667-7304, ์ด๋ฉ์ผ: ssac@sba.seoul.kr"
android:autoLink="web|email|phone"/>
• android:maxLines - ๋ฌธ์์ด์ด ํน์ ๋ผ์ธ ๊น์ง๋ง ์ถ๋ ฅ๋๊ฒ ์ค์
• ellipsize - ์ค์ํ์, end, start, middle
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/long_text"
android:maxLines="3"
android:ellipsize="end"/>
14๊ฐ ImageView
ImageView ์์ฑ
•android:src - ์ถ๋ ฅ๋ ์ด๋ฏธ์ง ์ง์
•maxWidth, maxHeight - ํ๋ฉด์ ์ถ๋ ฅ๋ ๋ทฐ์ ์ต๋ ์ฌ์ด์ฆ ์ง์
•adjustViewBounds - ์ข
ํก๋น ์ ์ง
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/lake"
android:maxWidth="100dp"
android:maxHeight="100dp"
android:adjustViewBounds="true"/>
scaleType
•ImageView ์ ์ถ๋ ฅ๋๋ ์ด๋ฏธ์ง ๋น์จ, ํฌ๊ธฐ ์ค์
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/lake"
android:scaleType="center"/>
•center : ์ด๋ฏธ์ง ์๋ณธ ์ฌ์ด์ฆ ์ ์ง, ์ด๋ฏธ์ง ์ค์์ ImageView ์ ์ถ๋ ฅ
•centerCrop : ์ด๋ฏธ์ง์ ๊ฐ๋ก/์ธ๋ก ์ค ์งง์ ์ชฝ์ ImageView ์ ๊ฝ ์ฐจ๊ฒ ์ถ๋ ฅ
•centerInside : ์ด๋ฏธ์ง์ ๊ฐ๋ก/์ธ๋ก ์ค ๊ธด ์ชฝ์ ImageView ์ ๊ฝ ์ฐจ๊ฒ ์ถ๋ ฅ
•fitStart : ๊ฐ๋ก/์ธ๋ก ๋น์จ์ ์ ์งํ์ฌ ImageView ์์ ์ถ๋ ฅ. ImageView ์ ์ข์ธก์๋จ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
•fitEnd : ๊ฐ๋ก/์ธ๋ก ๋น์จ์ ์ ์งํ์ฌ ImageView ์์ ์ถ๋ ฅ. ImageView ์ ์ฐ์ธกํ๋จ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
•fitCenter : ๊ฐ๋ก/์ธ๋ก ๋น์จ์ ์ ์งํ์ฌ ImageView ์์ ์ถ๋ ฅ. ImageView ์ ์ค์์ ๊ธฐ์ค์ผ๋ก์ ๋ ฌ
•fitXY : ๊ฐ๋ก/์ธ๋ก ๋น์จ์ ์๊ด์์ด ImageView ์ ๊ฝ ์ฐจ๊ฒ ์ถ๋ ฅ. ์ข
ํก๋น ๋ฌด์
Lab - ImageView ์์ฑ
15๊ฐ Button, EditText
Button
•Button : ์ ์ ์ด๋ฒคํธ ์ฒ๋ฆฌ๋ฅผ ์ํ View
•CheckBox : ๋ค์ค ์ ํ
•RadioButton : ๋จ์ผ ์ ํ, ๋์ผ RadioGroup ๋ด์์ ํ๋๋ง ์ ํ์ด ๊ฐ๋ฅ
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="radio1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="radio2"/>
</RadioGroup>
EditText
•android:lines – EditText ๊ฐ ํ๋ฉด์ ์ถ๋ ฅ๋ ๋ผ์ธ์
•android:maxLines – ์ฌ๋ฌ์ค ์
๋ ฅ์ ์ํด ๋์ด๋๋ ์ต๋ ๋ผ์ธ์
•android:inputType – ํค๋ณด๋ ์ ์ด, ํค๋ณด๋ ๋ชจ๋, ํ์ค ์
๋ ฅ ์ ์ด
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="phone number"
android:inputType="phone"/>