https://blog.naver.com/manhdh/220254628215

 

디버깅 기기가 offline으로 뜰 때 해결 방법 4가지

잘되던 연결이 갑자기 안된다하면,  클립스든, 인텔리 툴이든 둘다 적용되는 4가지 방법은 ...

blog.naver.com

 

내가 선택한 방식은 케이블 변경

충전 케이블에서 고속충전 데이터 케이블로 변경했다.

 

데이터 전송이 지원되지 않아서 

디바이스 인식이 PC에서는 되지만

안드로이드 스튜디오 디바이스 매니저에서는 되지 않는 것이었다.

 

 

다이소 3000원.

1) Constraint View

ConstraintLayout 에서 보기의 위치를 정의하려면

뷰의 가로 및 세로 제약조건을 각각 하나 이상 추가해야 한다.

 

각 뷰에는 축마다 하나 이상의 제약조건이 있어야 하며, 흔히 더 많이 필요하다.

 


2) 살펴보기

1) 프로젝트 생성하기

Empty Activity > chapter3 프로젝트 만들기

 

2) constraint 미지정 오류

2-1) textView 추가

activity_main.xml 에서 textView 를 추가해보자.

res > layout > acitivity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="안녕하세요"
         />

</androidx.constraintlayout.widget.ConstraintLayout>

2-2) constraint 미지정 오류 발생

만약 위와 같이 코드를 구성한다면

아래와 같은 오류가 발생한다.

 

 

This view is not constrained.

It only has designtime positions,

so it will jump to (0,0) at runtime unless you add the constraints.

 

뷰에 제약조건이 없다. designtime positions 만 존재한다.

Supress : Add tools: ignore = “MissingConstraints” attribute

제약조건을 설정하지 않을 것이라면, “MissingConstraints” 라는 attribute 를 추가해야 한다.


3) constraint 추가하기

constraint 을 추가하는 방법에는 2가지가 있다.

  1. Design 에서 화살표의 끝을 constraint 에 지정
  2. 코드로 작성

 

화살표를 부모뷰의 오른쪽으로 붙이면,

textView 가 오른쪽에 붙는다.

 

동시에 아래 코드가 생성된다.

app:layout_constraintEnd_toEndOf="parent"


3-1) 가로방향 constraint

좌측 (노란색) 은 뷰에 대한 내용

우측 (초록색) 은 제약조건에 대한 내용으로 생각하자.

 

 

layout_constraintEnd_toEndOf

  • layout_constraintEnd뷰의 가로방향 constraint 는 어디에 둘 것인가?

       뷰에 대한 내용.

       toEndOf부모의 끝을 제약조건으로 하겠다.

       어디를 제약조건으로 하겠다.


3-2) 세로방향 constraint

app:layout_constraintBottom_toBottomOf="parent"

 

  • layout_constraintBottom뷰의 세로방향 constraint 는 어디에 둘 것인가?
  • 뷰에 대한 내용.
  • toBottomOf부모의 아래를 제약조건으로 하겠다.
  • 어디를 제약조건으로 하겠다.

3-3) constraint 미지정 오류 해결

아래와 같이 가로/세로 constraint 이 모두 주어지면

constraint 지정하라는 오류가 사라진다.

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:text="안녕하세요"
        
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" 
/>

3-4) 4가지 방향 constraint 지정하기

1) ConstraintView 에서

각 뷰에는 축마다 하나 이상의 제약조건이 있어야 하며, 흔히 더 많이 필요하다.

라는 설명이 있었다.

 

위 코드에는 오른쪽/아래쪽 constraint 만 지정되어있다.

 

 

 

왼쪽/위쪽 constraint 도 지정할 수 있다.

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="안녕하세요"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

/>

이렇게 4가지 방향 constraint 를 모두 지정하면

textView 가 중앙에 배치된다.

 


3-5) bias 지정하기

constraint 를 지정하되, 중앙배열이 아닌

한 쪽으로 치우친 형태로 만들고 싶다면

 

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="안녕하세요"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.173"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.353" />

/>

위와 같이 bias 를 추가해주면 된다.

    app:`layout_constraintHorizontal_bias` = "0.173"
    app:`layout_constraintVertical_bias`     = "0.353" 

bias 는 0 (시작점) 부터 1 ( 끝점 ) 까지 범위가 있다.

app:layout_constraintVertical_bias = "0.3" 이면, 위쪽으로 치우친다.

 

app:layout_constraintVertical_bias = "0.7" 이면, 아래쪽으로 치우친다.


3) 전체코드

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="안녕하세요"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.67"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.407" />


    />

</androidx.constraintlayout.widget.ConstraintLayout>

=

1) 개요

  • 입력, 출력 단위를 선택할 수 있음
  • 입력 ↔ 출력 단위를 반대로 변경할 수 있음
  • 입력창에 값을 입력하면 바로 결과창에 단위가 변환된 값이 노출

2) 구현기능

  • cm 를 m 로 변환
  • 값을 입력하면, 바로 변환된 값이 노출
    • 입력값은 자연수로 한정
  • 단위를 반대로 변경
  • 단위 변환 연산
    • cm → m (X 0.01)
    • m → cm (X 100)

3) 학습 목표

  • 간단한 기능을 구현하고, UI 를 그릴 수 있다 (2)
    • ConstraintLayout 을 이용하여 간단한 UI 를 그릴 수 있다.
    • 키보드로 사용자가 입력한 값을 받을 수 있다.
    • 사용자의 입력값이 변경되면, 바로 변환된 값을 보여줄 수 있다.
    • 방향이 변경됐을때, 값을 유지하는 방법
  • UI
  • Android

 

 

 


[부연설명]

 

Constraint Layout

Linear Layout 은 레이아웃 중첩 때문에 Depth 가 커져서

렌더링 속도가 느려진다는 단점이 있다.

Flat 한 구조를 가지기 위한 Constraint Layout 사용

 

 

ViewBinding

findViewByID 를 통해

UI 요소를 코틀린으로 가져왔는데

ViewBinding 을 이용하면

조금 더 간단하고 쉽게 뷰를 가져올 수 있다.

 

 

OnSaveInstanceState

Counter App 에서 방향 전환 시

TextView 의 숫자 text 가 0으로 초기화되는 문제를 어떻게 해결할지?

onSaveInstanceState 를 통해 알아보자.

 

 

1) 복습


2) 한 걸음 더

    1. 값을 유지하려면 어떻게 해야할까요?
    2. 화면 방향에 상관없이 버튼을 보이게 하려면 어떻게 해야할까요?
  1. weight 를 넣을 때 dimension 에 왜 0dp 를 넣으라고 했을까요?

 

 

 

1) 기능 구현

  • var 타입의 number 변수를 두어numbertextView의 text 에 number 값 대입한다.
  • number 변수의 값을 변경한 후
  • textview 의 text 는 String 타입이고number.toString() 으로 형 변환하여 넣기
  • number 는 Int 타입이므로
var number = 0

// 동작에 대한 연산 처리
// 클릭에 대한 처리
resetButton.setOnClickListener{
    number = 0
    numberTextView.text = number.toString() // 숫자를 문자열로
}

2) 결과


3) 전체코드

Activity.kt

package com.part1.chapter2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // UI 요소 가져오기
        val numberTextView = findViewById<TextView>(R.id.numberTextView)
        val resetButton = findViewById<Button>(R.id.resetButton)
        val plusButton = findViewById<Button>(R.id.plusButton)

        var number = 0

        // 동작에 대한 연산 처리
        // 클릭에 대한 처리
        resetButton.setOnClickListener{
            number = 0
            numberTextView.text = number.toString() // 숫자를 문자열로
            // 리셋 버튼의 클릭에 대한 리스닝을 하고 있는지 확인하기 위해 Log 찍기
            Log.d("onClick", "리셋된 숫자는 $number") // 디버깅용이므로 Log.d
        }

        plusButton.setOnClickListener{
            // + 버튼의 클릭에 대한 리스닝을 하고 있는지 확인하기 위해 Log 찍기
            number++
            numberTextView.text = number.toString() // 숫자를 문자열로
            Log.d("onClick", "+된 숫자는 $number") // 만약 에러용이면 Log.e
        }
    }

}

main_activity.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">

    <TextView
        android:id="@+id/numberTextView"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:text="0"
        android:textSize="100dp"
        android:textColor="@color/blue"
        android:textStyle="bold|italic"
        android:gravity="center"/>

    <!-- Linear Layout 는 vertical 이다.
    그러나 버튼은 가로 정렬하고 싶다.
    이럴 때는, 중첩 레이아웃을 사용하면 된다. -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/resetButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="초기화"
            android:layout_weight="1"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="16dp"
            android:layout_marginStart="16dp"

            />

        <Button
            android:id="@+id/plusButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="+"
            android:layout_weight="1"
            android:layout_margin="16dp"
            />
    </LinearLayout>
</LinearLayout>

1) Activity

안드로이드 4대 요소 중 하나인 Activity

 

사용자가 앱과 상호작용하기 위한 진입점

 

 

화면이 있고 유저에게 입력값을 받을 수 있는 부분이 있고

입력받은 값을 보여주는 화면이 있다.

 

Activity 를 능숙하게 관리하고 싶을 때

반드시 알아야하는 것 중 하나가

Activity 의 Life cycle (수명주기) 이다.

 

상태변경에 이를 수 있는 방법이나

앱 간의 이동, 앱 간의 데이터 사이 관계도 알아야 한다.

 

이런 부분은 부록을 통해 학습 가능.


2) UI 요소 가져오고, 동작 처리 확인하기

2-1) 각 View 의 id 설정

@+id 를 사용하면 아이디를 추가한다는 의미이다.

id=”@+id/…”

숫자 텍스트뷰와, 초기화 버튼, +버튼에 id 를 주자.

<TextView
android:id="@+id/numberTextView"

...
<Button
            android:id="@+id/resetButton"
...
<Button
            android:id="@+id/plusButton"

2-2) UI 요소 가져오기 ( findViewByID )

findViewById<TextView>(R.id.numberTextView)

// UI 요소 가져오기
        val numberTextView = findViewById<TextView>(R.id.numberTextView)
        val resetButton = findViewById<Button>(R.id.resetButton)
        val plusButton = findViewById<Button>(R.id.plusButton)


2-3) 동작에 대한 연산처리 확인하기 ( Log.d )

        // 동작에 대한 연산 처리
        // 클릭에 대한 처리
        resetButton.setOnClickListener{
            // 리셋 버튼의 클릭에 대한 리스닝을 하고 있는지 확인하기 위해 Log 찍기
            Log.d("onClick", "리셋 버튼이 클릭되었습니다.") // 디버깅용이므로 Log.d
        }

        plusButton.setOnClickListener{
            // 리셋 버튼의 클릭에 대한 리스닝을 하고 있는지 확인하기 위해 Log 찍기
            Log.d("onClick", "+ 버튼이 클릭되었습니다.") // 만약 에러용이면 Log.e
        }

여러 가지 디버깅 수준으로 Log 를 찍을 수 있는데

그 중 Log.d 는 디버깅용이고

그 중 Log.e 는 에러용이다. ( 빨간글씨 )

 

Log 는 Android Studio 의

Logcat 에서 확인할 수 있다.


2-4) 전체 코드

MainActivity.kt

package com.part1.chapter2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // UI 요소 가져오기
        val numberTextView = findViewById<TextView>(R.id.numberTextView)
        val resetButton = findViewById<Button>(R.id.resetButton)
        val plusButton = findViewById<Button>(R.id.plusButton)

        // 동작에 대한 연산 처리
        // 클릭에 대한 처리
        resetButton.setOnClickListener{
            // 리셋 버튼의 클릭에 대한 리스닝을 하고 있는지 확인하기 위해 Log 찍기
            Log.d("onClick", "리셋 버튼이 클릭되었습니다.") // 디버깅용이므로 Log.d
        }

        plusButton.setOnClickListener{
            // + 버튼의 클릭에 대한 리스닝을 하고 있는지 확인하기 위해 Log 찍기
            Log.d("onClick", "+ 버튼이 클릭되었습니다.") // 만약 에러용이면 Log.e
        }
    }

}

 

 

 

activity_main.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">

    <TextView
        android:id="@+id/numberTextView"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:text="0"
        android:textSize="100dp"
        android:textColor="@color/blue"
        android:textStyle="bold|italic"
        android:gravity="center"/>

    <!-- Linear Layout 는 vertical 이다.
    그러나 버튼은 가로 정렬하고 싶다.
    이럴 때는, 중첩 레이아웃을 사용하면 된다. -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/resetButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="초기화"
            android:layout_weight="1"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="16dp"
            android:layout_marginStart="16dp"

            />

        <Button
            android:id="@+id/plusButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="+"
            android:layout_weight="1"
            android:layout_margin="16dp"
            />
    </LinearLayout>
</LinearLayout>

1) 결과 코드와 결과 화면

<?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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:text="100"
        android:textSize="100dp"
        android:textColor="@color/black"
        android:textStyle="bold|italic"
        android:gravity="center"/>

    <!-- Linear Layout 는 vertical 이다.
    그러나 버튼은 가로 정렬하고 싶다.
    이럴 때는, 중첩 레이아웃을 사용하면 된다. -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="초기화"
            android:layout_weight="1"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="16dp"
            android:layout_marginStart="16dp"

            />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="+"
            android:layout_weight="1"
            android:layout_margin="16dp"
            />
    </LinearLayout>
</LinearLayout>

 

 


2) 속성값

1) color

color 값은 app > res > values > colors.xml 에 정의되어 있음

원하는 색도 추가할 수 있음

<color name="white">#FFFFFFFF</color>

 

왼쪽 파란 원 안에 있는 파랑색 네모 (색깔) 클릭하면,

팔레트에서 색 지정 가능함


2) gravity

정렬 지정

 

2-1) gravity

view 자체의 위치를 정렬

 

 

2-2) layout_gravity

linear layout 이 부모로 있는 child 들의 속성으로,

부모 정렬을 기준으로 함

 

‘|’ 를 통해서 두 개의 속성값을 동시에 지정할 수도 있음

android:gravity="center_horizontal | center_vertical"

 

위 둘의 속성을 합친 것이 “center”


3) layout_weight

컴포넌트 비율 지정

android:layout_weight=“1"

 

원래 버튼이 있었던 공간이 아니라

버튼이 차지하고 남아있던 공간을 다 가져간다.

 

android:layout_weight=“1”

android:layout_weight=“1”

공간 두 개를 같은 크기로 나눈다. 절반으로.

 

layout_weight 는 상대적이어서

절반으로 나눈다면, 숫자만 같으면 된다.

android:layout_weight=“0.5”

android:layout_weight=“0.5”

 

 

layout_weight 를 지정할 때는

width 또는 height 가 반드시 0일 때 잘 적용이 된다.

 

 

orientation 이 horizontal 이면 width 기준이다.

horizontal 이라는 것은 가로로 화면을 쌓는 것이므로

뷰 기준으로는 “폭” 에 영향을 미친다.

남아있는 공간을 0dp 로 사용하는 것이다.

 

vertical 이면 height 기준이다.


4) margin 과 padding

<Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="초기화"
            android:layout_weight="1"
            android:layout_margin="16dp"/>
<Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="+"
            android:layout_weight="1"
            android:padding="16dp"
            />

좌: layout_margin

우: padding

 

 

화면에 너무 꽉 차있다는 느낌이 든다면

layout_margin 과 layout_padding 값을 조절하자

 

4-1) margin

상/하/좌/우 모두 띄우겠다

뷰 기준으로 상위 레이아웃에서 띄우겠다

 

4-2) padding

상/하/좌/우 모두 띄우겠다

자기 자신(뷰) 을 기준으로 띄우겠다.

 

만약 두 버튼 모두 margin 설정을 하였다면

버튼 사이 공간이 32 dp 가 되어 버린다.

 <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="초기화"
            android:layout_weight="1"
            android:layout_margin="16dp"/>
        
<Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="+"
            android:layout_weight="1"
            android:layout_margin="16dp"
            />

 

이런 문제를 해결하기 위해

각각의 margin 을 개별적으로 설정하려면?

 

android:layout_marginTop="16dp"

android:layout_marginBottom="16dp"

android:layout_marginLeft="16dp"

이런 식으로 하면 되는데,

margin_Left 에서 아래와 같은 경고 발생한다.

 

 

right to left 도 support 가능하다.

Replace layout_marginLeft with layout_marginStart

 

 

Android 는 다양한 앱과 다양한 국가에서 지원한다.

대부분의 언어는 left to right 로 읽는다.

 

아랍국가 같은 경우는 right to left 로 읽으므로

레이아웃을 오른쪽에서 왼쪽으로 그리는 케이스이다.

이러면 left 와 right 가 바뀌게 된다.

 

 

따라서 layout_marginLeft 를 marginStart 로의 변경 권장 경고가 뜬다.


5) 단위와 pixel

단위

크기를 지정할 때, 4가지 종류를 사용한다.

5-1) match_parent

부모 기준으로 꽉 채운다.

5-2) wrap_content

content 를 채울 수 있을 만큼 채운다.

5-3) dp ( density independent pixel )

기기와 관계 없이 pixel 크기가 동일

 

 

안드로이드 기기는 화면의 크기가 다르지만

개발자 입장에서는 최대한 각 기기에서

유사한 공통된 UI 를 제공하고 싶다.

 

 

점 하나를 pixel 이라고 한다.

왼쪽 휴대폰과 오른쪽 휴대폰의 pixel 개수가 다르다.

그러나 우리는 똑같은 크기의 UI 를 보여주고 싶다.

이럴때, dp 를 사용한다.

 

 

 

밀도 독립형 픽셀 ( density independent pixel )

안드로이드에서 크기를 지정할 때는 거의 모두 dp 를 사용한다.

 

 

추가설명) pixel 과 dp 의 차이점

pixel 은 기기에 따라서 크기가 다르다.

Dp 는 기기와 관계 없이 크기가 같다.


5-4) sp ( scalable pixel )

크기가 변경이 가능한 픽셀

 

 

안드로이드 설정에서 폰트 크기를 설정 가능하다.

이때 설정 폰트 크기에 따라서 sp 로 지정한 것은

글자 크기 변경이 된다.

 

 

Font Setting 과 함께 글자 크기를 가져갈 수 있게 하기 위해서

Sp 를 textsize 에 사용하는 것을 권장함.

 

 

현업에서는 글자 크기를 변경하고 싶지 않은 케이스가 있는데,

그런 경우에는 Font setting 을 변경하여도

글자 크기가 변경되지 않는다.

 

 

단, sp 를 사용하라는 warning 이 발생할 수 있음

Layout

viewGrop 이라는 아주 큰 도화지 아래에

작은 도화지 그룹인 viewGrop 이 있고

그림요소인 View 들이 각각에 존재한다-고 생각할 수 있다.

 

Palette 에서 요소 Drag & Drop 할 수 있음

 

 

app > res > layout > activity_main.xml

리소스 폴더 (res) : 화면을 그리기 위한 다양한 요소들이 있는 폴더

 

0) activity_main.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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:srcCompat="@tools:sample/avatars" />



    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

 

1) LinearLayout 설정

<LinearLayout

 

 

2) match_parent 와 wrap_content

부모뷰의 크기에 맞추기 : match_parent

android:layout_width="match_parent"
android:layout_height="match_parent"

요소 크기에 맞추기 : wrap_content

android:layout_width="wrap_content"
android:layout_height="wrap_content"

 

 

3) orientation

요소들을 세로로 쌓으려면

android:orientation="**vertical**"

요소들을 가로로 쌓으려면

android:orientation="**horizontal**"

 

 

4) 만약

위 코드에서 orientation 을 horizontal 로 바꾼다면

android:orientation="horizontal"

세로로 쌓이던 요소가 가로로 쌓인다.

Textview 에서 layout_width가 match_parent 였으므로

Textview 오른쪽에 붙을 Button 요소는

화면에 보이지 않는다.

layout_width 를 wrap_content 로 변경한다면

요소의 크기만큼 가로길이가 지정된다.

Text 길이만큼.

 

+ Recent posts