728x90
반응형
View 클래스
- 안드로이드 화면에서 실제로 사용되는 것들은 모두 View 클래스 상속을 받는다.
- 뷰 클래스는 다른 말로 '위젯' 이라고도 한다.
- 다른 위젯을 담을 수 있는 위젯은 레이아웃이라고 한다.
- 레이아웃은 ViewGroup 클래스 아래 존재한다.
안드로이드 View 클래스 계층도
id 속성
- id 속성은 모든 위젯의 아이디를 나타낸다.
- Java 코드에서 위젯에 접근할 때 id 속성에 지정한 아이디를 사용한다.
- id 속성은 위젯에 아이디를 새로 부여하는 개념이므로 "@+id/" 형식으로 지정 /다음에는 새로 지정할 id를 적는다.
- android:id="@+id/btn1"의 의미는 버튼 위젯의 아이디를 btn1로 부여한 것이다.
예시)
위젯 변수 = (위젯형) findViewById(R.id.위젯id);
-> Button button1;
button1 = (Button) findViewById(R.id.btn1);
layout_width, layout_height 속성
- math_parent : 자신의 부모(대개는 레이아웃)에 폭이나 높이를 맞춘다는 의미
- wrap_content : 자신의 폭이나 높이를 자신 안의 글자가 들어갈 정도로 설정
padding 속성
- padding은 위젯의 경계선으로부터 위젯 안의 요소가 떨어지도록 설정
layout_margin 속성
- 위젯과 위젯 사이에 여유를 두고 싶다면 layout_margin 속성을 사용
visibility 속성
- visibility 속성은 위젯을 보일 것인지 여부를 결정
- 디폴트인 visible은 보이는 상태, invisible과 gone은 안보이는 상태를 의미
enabled, clickable 속성
- enabled 속성 - 위젯의 동작 여부
- clickable 속성 - 클릭이나 터치가 가능하도록 한다.
rotation 속성
- rotation은 위젯을 회전시켜 출력
- 값은 각도로 지정
기본 위젯 다루기
텍스트뷰
계층도
- text 속성 - 텍스트뷰에 나타나는 문자열 표현
- textColor 속성 - 글자의 색상을 지정, #RRGGBB나 #AARRGGBB 형식
- textSize 속성 - 글자의 크기를 dp, px, in, mm, sp 단위로 지정
- typeface 속성 - 글자의 글꼴을 지정 값으로 지정 sans, serif, monospace 설정, 디폴트는 normal
- textStyle - 글자의 스타일을 지정하는 값 bold, italic, bold|italic을 설정, 디폴트는 normal
- singleLine 속성 - 글이 길어 줄이 넘어갈 경우 강제로 한 줄만 출력하고 문자열의 맨 뒤에 "..."을 표시, 값으로 true,false를 설정할 수 있으며 디폴트는 false
xml 속성과 관련 메소드
클릭 이벤트 활용 예제
JAVA
package com.example.a2c_1005_3;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.lang.reflect.Array;
public class MainActivity extends AppCompatActivity {
Button btnFirst, btnSecond, btnThird, btnRun;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnFirst = findViewById(R.id.btnFirst);
btnSecond = findViewById(R.id.btnSecond);
btnThird = findViewById(R.id.btnThird);
btnRun = findViewById(R.id.btnRun);
//btnRun 이벤트 발생
btnRun.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
btnFirst.setText("안녕하세요");
btnFirst.setTextSize(24);
btnFirst.setTextColor(Color.parseColor("#FF0000"));
btnSecond.setText("두번째 안녕");
btnSecond.setTextSize(24);
btnThird.setText("세번째 안녕");
btnThird.setTextSize(30);
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btnFirst"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnSecond"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnThird"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnRun"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
버튼 클릭 전
버튼 클릭 후
728x90
반응형
최근댓글