안드로이드에 화면에서 실제로 사용되는 것들은 모두 View 클래스 상속을 받는다
다른 위젯을 담을 수 있는 위젯 : 레이아웃
레이아웃도 크게 보면 위젯에 포함된다.
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="버튼입니다"/>
id 속성: 위젯의 아이디
java 코드에서 위젯에 접근할 때 사용
android:id = "@+id/btn1" : 아이디를 btn1으로 부여
Button, RadioButton, CheckBox 등의 위젯은 일반적으로 클릭 또는 터치했을 때 어떤 동작을 위한 것이므로 id 속성을 지정한다.
클릭이나 터치를 해도 동작이 필요 없는 텍스트뷰, 이미지뷰 등은 굳이 id 속성을 지정하지 않아도 된다.
위젯 변수 = (위젯형) findViewById(R.id.위젯id);
Button button1; button1 = (Button) findViewById(R.id.btn1);
match_parent : 부모(대개는 레이아웃)에 맞춤
wrap_content : 콘텐츠(글자, 이미지)에 맞춤
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
위젯의 색상을 주로 #RRGGBB 값으로 지정
<Button
android:background="#ff0000"/>
텍스트뷰 1개와 버튼 1개가 있는 기본적 activity_main.xml 전체 코드
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
위젯을 보일 것인지 여부 결정
visible : 보이는 상태invisible : 안 보이는 상태 (자리 유지)gone : 안 보이는 상태 (자리까지 없어짐)enabled : 위젯의 동작 여부
clickable : 클릭이나 터치가 가능하도록 함
true(디폴트), false로 지정
BUT xml 보다 java 코드에서 주로 지정
<Button
android:enabled="false"/>
위젯을 회전 시킴
<Button
android:rotation="45"/>