Android

[Android] LinearLayout 에 대해서 알아보기

YaluStar 2013. 12. 31. 00:16

이번에는 LinearLayout 에 대해서 알아보도록 하겠습니다

 

 

LinearLayout 

             선형으로 뷰들을 관리하는 레이아웃

                => 선형 배치 관리자

 

상속계층도

java.lang.Object

 => android.view.View

   => android.view.ViewGroup

      => android.widget.LinearLayout

 

기본적으로 사용하는 속성으로는

  android:layout_width=""

  android:layout_height=""

여기에서 LinearLayout 에서 사용하는것은

 android:orientation="" 속성 과

android:layout_weight="" 속성이다

 

 

android:orientation 

   이 속성에는 vertical 과 horizontal 2가지가 있으며

    vertical 은 뷰들을 세로로 배치하고,

    horizontal 은 뷰들을 가로로 배치한다. 

위 사진은 orientation = "horizontal" 속성을 사용한 것이다

뷰들을 배치할때 화살표 방향인 가로방향으로 배치를 한다.

 

 

이 사진은 orientation = "vertical" 속성을 사용한 것인데

뷰들을 배치할 때 세로 방향으로 배치를 하게 된다.

 

 

android:layout_weight = "" 

   레이아웃의 가중치를 지정하는 속성이다

  이 속성을 가진 뷰를 포함하고 있는 상위 뷰에서 뷰들의

  배치 비율를 정하는데 사용한다

 

Example

각각 숫자가 써져 있는데

가중치를 2 / 1 / 1 로 설정한 것이다

이렇게 설정을 하면 뷰안에서 주황색은 뷰의 2칸  파란색과 갈색은 1칸 씩 차지하게 된다.

 

 

문제를 내볼테니 한번 풀어보세요

답은 좀만더 아래로 내리면 있습니다.

색은 상관없으니 그냥 하시면 됩니다.

각 숫자는 weight 에 해당하는 숫자들입니다.

 

 

 

 

 

 

 

 

----------------------------------------정 답---------------------------------------------

 

 

정답 소스

 <LinearLayout

     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:layout_weight="2"

           android:background="#0000FF" />

 

       <TextView

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

           android:layout_weight="1"

          android:background="#0FFFFF" />

 

       <TextView

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

           android:layout_weight="3"

          android:background="#00FF00" />

 

       <TextView

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

           android:layout_weight="4"

          android:background="#0F00F0" />

</LinearLayout>

 

 

 

이상으로 LinearLayout 에 대해서 알아보았습니다.

 

반응형