[Android Studio] View 클래스

ㅎㅎ·2023년 3월 26일

Android

목록 보기
3/6

View 클래스

안드로이드에 화면에서 실제로 사용되는 것들은 모두 View 클래스 상속을 받는다

=> 위젯

  • 화면 속 버튼 : 버튼 위젯
  • 실제 코드 속 버튼 : 버튼 클래스

다른 위젯을 담을 수 있는 위젯 : 레이아웃
레이아웃도 크게 보면 위젯에 포함된다.


View 클래스의 xml 속성

<Button
	android:id="@+id/btn1"
    android:layout_width="wrap_content"
	android:layout_height="wrap_content"
    android:background="#ff0000"
    android:text="버튼입니다"/>

id 속성 지정

id 속성: 위젯의 아이디
java 코드에서 위젯에 접근할 때 사용

android:id = "@+id/btn1" : 아이디를 btn1으로 부여

Button, RadioButton, CheckBox 등의 위젯은 일반적으로 클릭 또는 터치했을 때 어떤 동작을 위한 것이므로 id 속성을 지정한다.

클릭이나 터치를 해도 동작이 필요 없는 텍스트뷰, 이미지뷰 등은 굳이 id 속성을 지정하지 않아도 된다.

위젯 변수 = (위젯형) findViewById(R.id.위젯id);

Button button1;
button1 = (Button) findViewById(R.id.btn1);

layout_width, layout_height 속성

match_parent : 부모(대개는 레이아웃)에 맞춤
wrap_content : 콘텐츠(글자, 이미지)에 맞춤

<Button
    android:layout_width="match_parent"
	android:layout_height="wrap_content"/>

background 속성

위젯의 색상을 주로 #RRGGBB 값으로 지정

<Button
    android:background="#ff0000"/>

기본적인 xml 코드

텍스트뷰 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>

visibility 속성

위젯을 보일 것인지 여부 결정

  • visible : 보이는 상태
  • invisible : 안 보이는 상태 (자리 유지)
  • gone : 안 보이는 상태 (자리까지 없어짐)

enabled, clickable 속성

enabled : 위젯의 동작 여부
clickable : 클릭이나 터치가 가능하도록 함

true(디폴트), false로 지정

BUT xml 보다 java 코드에서 주로 지정

<Button
    android:enabled="false"/>

rotation 속성

위젯을 회전 시킴

<Button
    android:rotation="45"/>
profile
Backend

0개의 댓글