App노자

[Android] View 클래스 본문

Android/AndroidStudio

[Android] View 클래스

앱의노예 2022. 6. 6. 15:03

1. View란?


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

모든 뷰 클래스의 최상위 클래스이다 액티비티는 view의 서브클래스만 화면에 출력한다

액티비티 화면을 구성할 때 사용하는 클래스는 모두 View의 하위 클래스이다 그래서 화면 구성과 관련한 클래스를 

통칭하여 뷰클래스라고 한다

버튼, 텍스트뷰, 이미지 등 모두 View의 View라는 클래스의 상속을 받는 서브 클래스이며 

View 클래스를 담을 수 있는 클래스를 레이아웃이라고 한다

레이아웃의 경우 ViewGroup클래스의 아래에 존재한다

View의 하위클래스지만 자체 UI는 없어서 화면에 출력해도 아무것도 나오지 않는다

다른 뷰 여러개를 묶어서 제어할 목적으로 사용한다 일종의 그릇 역할을 한느 클래스로 

 

 

 

 

 

 

https://developer.android.com/reference/android/view/View

 

View  |  Android Developers

 

developer.android.com

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack{
            Text("Hello, SwiftUI \nStudy")   //코드로 작성하는 내용이 즉시 반영되고 있음
                .font(.title)   //폰트 크기 스타일
                .fontWeight(.thin)   //폰트 굵기
                .foregroundColor(.black)   //색깔
                .lineLimit(1)   //문자열의 길이를 제한
            
            Text("VStack은 세로 방향으로 뷰를 배열함")
                .foregroundColor(.white)   //글자색
                .padding(10)   //주변의 여백 설정
                .background(Color.black)
            
            Text("커스텀 폰트, 볼드체 밑줄, 취소선")
                .font(.custom("Size20", size: 20))
                .bold()
                .italic()
                .underline()  //밑줄
                .strikethrough()   //텍스트에 취소선
            
            Text("라인 수 제한과 \n텍스트 정렬 기능입니다 \nTest lineLimit")
                .lineLimit(2)
                .multilineTextAlignment(.center)   //정렬 방식 지정
                .fixedSize()   //주어진 공간의 크기가 작아도 텍스트를 생략하지 않고 표현
                //trailing 오른쪽 정렬
                //leading 왼쪽에 정렬
                //center 중앙에 정렬
            (Text("Test").kerning(8)
             + Text("기준선 조정").baselineOffset(8))
            .font(.system(size:30))
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .previewInterfaceOrientation(.portrait)
    }
}

 

public protocol View {

    /// The type of view representing the body of this view.
    ///
    /// When you create a custom view, Swift infers this type from your
    /// implementation of the required ``View/body-swift.property`` property.
    associatedtype Body : View

    /// The content and behavior of the view.
    ///
    /// When you implement a custom view, you must implement a computed
    /// `body` property to provide the content for your view. Return a view
    /// that's composed of built-in views that SwiftUI provides, plus other
    /// composite views that you've already defined:
    ///
    ///     struct MyView: View {
    ///         var body: some View {
    ///             Text("Hello, World!")
    ///         }
    ///     }
    ///
    /// For more information about composing views and a view hierarchy,
    /// see <doc:Declaring-a-Custom-View>.
    @ViewBuilder var body: Self.Body { get }
}

view를 표헌하는 타입이 구조체로 바뀌고 프로토콜로 바뀌었다
클래스를 상속 받아 필요하지 않은 부분의 프로퍼티를 보유해야 했던 UIkit과 다르게
필요한 속성만 가지고 뷰를 생성할 수 있게 되었다

 

-  font() : 폰트의 크기 스타일을 설정한다

-  fontWeight() : 폰트의 굵기를 설정한다 

- foregroundColor() : 폰트의 색깔을 설정한다

- lineLimit() : 문자열의 길이를 제한한다 긴 Text는 .....으로 표현

- padding() : 주변의 여백 설정

'Android > AndroidStudio' 카테고리의 다른 글

[Android] LinearLayout  (0) 2022.06.16
[Android] EditText  (0) 2022.06.15
[Android] Button, CheckBox, RadioButton  (0) 2022.06.11
[Android] TextView  (0) 2022.06.08
[Android] Activity  (0) 2022.05.31