상세 컨텐츠

본문 제목

[Android] 뷰

Android 개발

by mobile 2011. 7. 26. 01:51

본문

반응형
사용자 인터페이스 구성을 위해 사용할 수 있는 클래스들을 대표하는 이름이며, 일반적으로 View 클래스를 상속한 대부분의 클래스들을 지칭한다. 그래서 안드로이드 API를 살펴보면 android.view.View 클래스를 상속하는 클래스가 매우 많다는 것을 알 수 있다. 가장 많이 사용하는 클래스들은 다음과 같다.

- TextView
: 텍스트 뷰는 화면에 고정된 텍스트 문자열을 표시하는 뷰 이다. 이 뷰의 문자열은 사용자가 수정할 수 없다.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView  

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="android developer site http://developer.android.com"

    android:lines="2"

    android:textSize="12sp"

    android:autoLink="web|email"

    />

</LinearLayout>


wrap_content : 포함된 내용을 보여줄 수 있는 크기로 설정
fill_parent : 부모 뷰의 크기와 동일한 크기로 설정
match_parent : fill_parent 와 동일

 [안드로이드 치수]
 단위 설명 
 px 픽셀(pixel)을 의미하며, 화면의 밀도와는 상관없는 치수이다. 
 dip
 dp 
dip는 density-independent pixel의 약자이며, 여러 밀도의 화면에서 일정한 크기를 보여줄 수 있도록 제공되는 치수 이다. dip를 줄여서 dp라고도 쓴다. pixel과 dip는 관계는 pixels = dips * (density/160)로 정의 된다. 그래서 160dpi 화면에서는 1dip는 1pixel이고 , 240dpi 화면에서는 1dip가 1.5pixel이 된다. 
 sp sp는 scale-independent pixel의 약자이며, dp와 유사한 기능을 한다. 추가적으로, 사용자 설정에 따라 변경되는 텍스트 크기를 반영해 준다.
 pts 포인트(points)의 약자이며, 1포인트는 1/72인치이다. 
 in 인치(inches)의 약자이다.
 mm 밀리미터(millimeters)의 약자이다. 
 
 
- EditText
: 에디트 텍스트는 사용자에게 입력을 받을 수 있는 뷰이며, 입력 기능을 제외 하고는 텍스트뷰와 동일하다.
▷ TextView의 하위 클래스 
▷ 입력 기능을 제외하고는 TextView와 동일함
▷ 화면을 오래 눌러 컨텍스트 메뉴 실행 가능(복사, 잘라내기, 붙여넣기)

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <EditText

    android:id="@+id/EdiText1"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:hint="type here"

    android:editable="false"

    android:singleLine="false" 

    />

</LinearLayout>

android:hint="type here" : 사용자 입력 전에 보여지며 입력하면 사라짐
android:editable="false" : 사용자 입려을 금지함
android:singleLine="false"  : 자동으로 라인을 아래로 내림


- Spinner
: 스피너는 사용자가 여러개의 아이템 중에서 한 개를 선택할 수 있게 해주는 뷰이다. 
▷ 아이템들은 어댑텨나 배열로 설정 가능
▷ getSelectedView() 메소드로 선택한 텍스트를 가지고 올 수 있음
▷ XML의 android:prompt 속성에는 반드시 문자열 자원 참조를 설정 해야 함

// main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <Spinner

    android:id="@+id/spinner1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:entries="@array/numbers" 

    />

</LinearLayout> 


/* /res/values/arrays.xml */

<?xml version="1.0" encoding="UTF-8"?>

<resources>

<string-array name="numbers">

<item>1</item>

<item>2</item>

<item>3</item>

<item>4</item>

<item>5</item>

</string-array>

</resources> 


- Button
: 버튼은 누를 수 있는 버튼들의 가장 일반적인 형태로 텍스트를 버턴에 보여준다.
▷ 기본적으로 텍스트를 보여주는 모양을 가짐
▷ 배경을 다른 모양을 설정해서 다른 모양의 버튼을 만들 수 있음

<Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="this is button"

    />


- ImageButton
: 이미지 버튼은 이미지를 설정할 수 있는 버튼인다.
▷ Button과 기능이 비슷함
▷ 이미지를 설정할 수 있음

<ImageButton 

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:src="@drawable/icon"

    />


- CheckBox
: 체크박스는 "체크됨"과 "체크되지 않음" 두개의 상태를 표시하는 버튼이다.
▷일반적으로 V 모양으로 체크 여부를 표시함

<CheckBox
      android:layout_width="wrap_content"

      android:layout_height="wrap_content"
      android:text="check" 

/> 


 - ToggleButton
: 토글 버튼은 전원 스위치 처럼 "ON", "OFF"를 표시하는 버튼이다.
▷ 체크박스와 비슷하면서 모양만 다른 버튼임

<ToggleButton 

android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:text="toggle button"

      android:textOn="On"

      android:textOff="Off"

/>

android:text="toggle button" 
: 의미 없는 문자열 - ToggleButton이 TextView를 상속하기 때문에 이렇게 선언해도 전혀 문제가 발생되지 않치만 실제로 화면에 표시되지 않는다.
android:textOn="On"
: 상태가 On일 경우에 표시되는 문자열 

android:textOff="Off"
: 상태가 Off 일 경우 표시되는 문자열 


- RadioButton
:라디오 버튼은 여러 개 중에서 한 개를 선택할 수 있는 선택 버튼이다.
▷ RadioGroup으로 묶었을 경우, 사용자가 일단 선택하면 다시 선택해도 해지 되지 않음
▷ 일반적으로 Radio Group으로 여러개의 RadioButton을 묶어 한개의 RadioButton만이 선택되게 함

<RadioGroup 

android:layout_width="wrap_content"

android:layout_height="wrap_content">

<RadioButton 

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Option 1"/>

<RadioButton 

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Option 2"/>

<RadioButton 

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Option 3"/>

<RadioButton 

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Option 4"/>

</RadioGroup>




반응형

관련글 더보기

댓글 영역