Layout 공식 문서

 

https://developer.android.com/guide/topics/ui/declaring-layout?hl=ko#layout-params

 

 

뷰의 레이아웃  |  Views  |  Android Developers

레이아웃은 활동 또는 앱 위젯의 UI와 같은 사용자 인터페이스의 시각적 구조를 정의합니다. 두 가지 방법으로 레이아웃을 선언할 수 있습니다. Android 프레임워크를 통해 이 두 가지 메서드의 하

developer.android.com

 

리니어레이아웃은 가로/세로로 쌓는 레이아웃. Orientation 으로 가로세로 결정

 

주요 설정 속성들

orientation = vertical, horizontal


자식들 설정
layout_gravity = 부모 레이아웃 에서 위치조정
gravity  = 자기 사이즈 안에서 위치조정. center,등

layout_weight = 
하나만 1 넣으면 남은공간 다 차지함. 
orientation 에 맞춰서 width 나 height 둘중 하나는 0dp 로 맞춰줘야함
디폴트는 0임.
각각의 weight 상대 비율대로 조정됨.
weight 로 꽉 채운 후에 마진,패딩을 주면 됨.

 

 

또한 안드로이드에서 코드로 아이템 추가가 가능함.

계속 추가될 것을 생각하여 스크롤 뷰 안에 리니어 레이아웃을 만들어서 사용

// XML 코드
<ScrollView
        android:id="@+id/lapScrollView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/guideline1"
        android:layout_marginTop="50dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:paddingBottom="16dp"
        >
        <LinearLayout
            android:id="@+id/lapContainerLinearLayout"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
        
</ScrollView>


//kotlin. 액티비티에서 아이템 추가
// 리니어 레이아웃 초기화
val container = binding.lapContainerLinearLayout

// 텍스트뷰 추가
TextView(this).apply {
    textSize = 20f
    gravity = Gravity.CENTER

    val minutes = currentDeciSecond.div(10)/60
    val seconds = currentDeciSecond.div(10)%60
    val deciSeconds = currentDeciSecond%10

    text = container.childCount.inc().toString() + String.format(
        "%02d:%02d %01d",
        minutes,seconds, deciSeconds
    )

    // 1. 01:03 0

    setPadding(30)

}.let { lapTextView ->
    container.addView(lapTextView, 0)  // 인덱스 0 에 추가.
}

+ Recent posts